Last active
March 19, 2021 23:10
-
-
Save esbenr/db24cf6c24203700187ba99801611e31 to your computer and use it in GitHub Desktop.
Parsing and extracting values from array in jinja templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----- We have an input_number sensor with the currently selected index ----- | |
states.input_number.color_index_garden.state = 1.0 | |
input_number: | |
color_index_garden: | |
name: Valgt farveindex i haven | |
initial: 0 | |
max: 255 | |
min: 0 | |
----- We have a file sensor with a list of colors in the state ----- | |
states.sensor.colors_common.state = "[[255,240,200],[255,230,170],[255,220,140],[255,0,0],[255,127,0],[255,255,0],[127,255,0],[0,255,127],[0,127,255],[0,0,255],[127,0,255],[255,0,255],[255,0,127]]" | |
sensor: | |
- platform: file | |
name: Colors Common | |
file_path: /config/www/files/colors_common.txt | |
----- Template playground ----- | |
More clever way with regex: | |
Colors: {{ states.sensor.colors_common.state }} | |
Colors count: {{ states.sensor.colors_common.state.split("],") | count }} | |
{% set index_entity_id = "input_number.color_index_garden" %} | |
Current index: {{ states(index_entity_id) | int }} | |
Current color: {{ states.sensor.colors_common.state | regex_findall_index("(\[\d{1,3},\s?\d{1,3},\s?\d{1,3}\])", index=states(index_entity_id) | int) }} | |
Next index: {{ 0 if states(index_entity_id) | int + 1 > states.sensor.colors_common.state.split("],") | count - 1 else states(index_entity_id) | int + 1 }} | |
Previous index: {{ states.sensor.colors_common.state.split("],") | count - 1 if states(index_entity_id) | int - 1 < 0 else states(index_entity_id) | int - 1 }} | |
----- Scripts for next/previous ----- | |
Script that handle button press for next/previous color | |
next_color: | |
alias: Næste farve | |
sequence: | |
- service: input_number.set_value | |
data_template: | |
entity_id: input_number.color_index_garden | |
value: '{{ 0 if(states.input_number.color_index_garden.state | int) + 1 > ((states.sensor.colors_common.state.split("],") | count) - 1) else (states.input_number.color_index_garden.state | int) + 1 }}' | |
- service: light.turn_on | |
data_template: | |
entity_id: "{{ target_entity_id }}" | |
rgb_color: '{{ states.sensor.colors_common.state | regex_findall_index("(\[\d{1,3},\s?\d{1,3},\s?\d{1,3}\])", index=(0 if(states.input_number.color_index_garden.state | int) + 1 > ((states.sensor.colors_common.state.split("],") | count) - 1) else (states.input_number.selected_index_garden.state | int) + 1)) }}' | |
previous_color: | |
alias: forrige farve | |
sequence: | |
- service: input_number.set_value | |
data_template: | |
entity_id: input_number.color_index_garden | |
value: '{{ ((states.sensor.colors_common.state.split("],") | count) - 1) if ((states.input_number.color_index_garden.state | int) - 1) < 0 else ((states.input_number.color_index_garden.state | int) - 1) }}' | |
- service: light.turn_on | |
data_template: | |
entity_id: "{{ target_entity_id }}" | |
rgb_color: '{{ states.sensor.colors_common.state | regex_findall_index("(\[\d{1,3},\s?\d{1,3},\s?\d{1,3}\])", index=((states.sensor.colors_common.state.split("],") | count) - 1) if ((states.input_number.color_index_garden.state | int) - 1) < 0 else ((states.input_number.selected_index_garden.state | int) - 1)) }}' | |
----- Example on buttons to page through colors ----- | |
- type: vertical-stack | |
cards: | |
- entity: light.garden | |
type: entity-button | |
name: "Have" | |
icon_height: 40px | |
hold_action: | |
action: more-info | |
tap_action: | |
action: toggle | |
- type: horizontal-stack | |
cards: | |
- entity: light.garden | |
type: entity-button | |
show_name: false | |
icon: mdi:skip-backward | |
icon_height: 30px | |
tap_action: | |
action: call-service | |
service: script.previous_color | |
service_data: | |
target_entity_id: light.garden | |
- entity: light.garden | |
type: entity-button | |
show_name: false | |
icon: mdi:skip-forward | |
icon_height: 30px | |
tap_action: | |
action: call-service | |
service: script.next_color | |
service_data: | |
target_entity_id: light.garden | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment