What do you mean by “two-step template”? Can you post which one you mean?
what I meant was debugging/ evaluating the output of an expression like:
scene.lights_step_{{ {18: 1, 21: 2, 23: 3, 26: 4}[trigger.above] }}
on the basis of :
- trigger:
- platform: numeric_state
entity_id: sensor.ambient_light
above: 18
- platform: numeric_state
entity_id: sensor.ambient_light
above: 21
- platform: numeric_state
entity_id: sensor.ambient_light
above: 23
- platform: numeric_state
entity_id: sensor.ambient_light
above: 26
so as to check the expected output of the expression if sensor.ambient_light is at say 24
You can use the json example with a little tweak in the template editor.
{% set my_test_json = {
"temperature": 18,
"unit": "°C"
} %}
scene.lights_step_{{{18: 1, 21: 2, 23: 3, 26: 4}[my_test_json.temperature]}}
Or more directly:
{% set trigger = {'above': 23} %}
scene.lights_step_{{ {18: 1, 21: 2, 23: 3, 26: 4}[trigger.above] }}
And, BTW, that example is not JSON. JSON is an object that has been serialized to be a single string representation of the data. (I only say this, not to pick on you, but because a lot of people seem to get confused as to what JSON is.) E.g., if you wanted to test JSON, then that would look something like this:
{% set value = {
"temperature": 26,
"unit": "°C"
}|to_json
%}
{{ value }}
{{ value is string }}
{% set value_json = value|from_json %}
{{ value_json }}
{{ value_json is string }}
The results of which are:
{"temperature": 26, "unit": "\u00b0C"}
True
{'temperature': 26, 'unit': '°C'}
False
But the template doesn’t check what the light level is, only which trigger fired.
thanks…
I am still very new to HA (migrated from domoticz) so trying to figure the basics
It does however appear that the template editor feature has the ability pick true sensor values rather than using a static preset value …
The code you have shared sure is handy but I was wondering if there was a way to evaluate more complicated expressions (like your example) to see how they react to dynamic changes
The template editor constantly re-renders the template as you enter it. That’s why, sometimes as you’re entering a template it will say something about errors until you get it complete. So, say, you want to see how this renders with different values:
{% set trigger = {'above': 23} %}
scene.lights_step_{{ {18: 1, 21: 2, 23: 3, 26: 4}[trigger.above] }}
Just change the value in the first line. As you change it it will be re-rendered and the result on the right side of the page will update.
Or you could do something like this:
{% for value in [18, 21, 23, 26] %}
{% set trigger = {'above': value} %}
scene.lights_step_{{ {18: 1, 21: 2, 23: 3, 26: 4}[trigger.above] }}
{% endfor %}
Also if you have a template that references real entities, as they are changing, again, every time you change the template, the result will update, too. Sometimes I just put the cursor at the end and hit space, delete, space, delete, space, … To get it to update over and over again.
Does that answer your question?
It does - and that’s what helped me set up the following code that I am posting for posterity’s sake and for anyone who lands up here via google
- Setup a Input Select with options from 0-6
Trigger to set Input Select as it gets darker
- alias: Automated lights helper - Step Up
trigger:
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
above: 160
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
above: 180
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
above: 200
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
above: 220
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
above: 240
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
above: 260
action:
- service: input_select.select_option
entity_id: input_select.daylight_sensor
data_template:
option: '{{ {160: 1, 180: 2, 200: 3, 220: 4, 240:5, 260:6}[trigger.above] }}'
id: a3e71b32ba324ddf91904c3e9d05c3f3
and Trigger to set input select as it gets brighter
- alias: Automated lights helper - Step Down
trigger:
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
below: 250
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
below: 230
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
below: 210
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
below: 190
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
below: 170
- platform: numeric_state
entity_id: sensor.terrace_analog_a0
below: 150
action:
- service: input_select.select_option
entity_id: input_select.daylight_sensor
data_template:
option: '{{ {150: 0, 170: 1, 190: 2, 210: 3, 230:4, 250:5}[trigger.below] }}'
id: 72ec1271f1e349218fbf0dce5dcb5993
And the final action to activate the appropriate scene:
- id: '1595324184664'
alias: Auto Lights - Stepped Action
description: ''
trigger:
- platform: state
entity_id: input_select.daylight_sensor
condition:
- after: 09:00:00
before: '22:00:00'
condition: time
action:
- service: scene.turn_on
data_template:
entity:id: >
{% if is_state("input_select.daylight_sensor", "0") %}
scene.lights_step_0
{%-elif is_state("input_select.daylight_sensor", "1") %}
scene.lights_step_1
{%-elif is_state("input_select.daylight_sensor", "2") %}
scene.lights_step_2
{%-elif is_state("input_select.daylight_sensor", "3") %}
scene.lights_step_3
{%-elif is_state("input_select.daylight_sensor", "4") %}
scene.lights_step_4
{%-elif is_state("input_select.daylight_sensor", "5") %}
scene.lights_step_5
{%-elif is_state("input_select.daylight_sensor", "6") %}
scene.lights_step_6
{% else %}
none
{% endif %} ```
Out of curiosity, why did you use an input_select
in your solution?
FWIW, the action part of the last automation can be reduced to:
- service: scene.turn_on
data_template:
entity:id: >
{% if trigger.to_state.state in ['0', '1', '2', '3', '4', '5', '6'] %}
scene.lights_step_{{ trigger.to_state.state }}
{% else %}
none
{% endif %}
Or if you think it’s ok to rely on the input_select
always having a valid choice, then:
- service: scene.turn_on
data_template:
entity:id: "scene.lights_step_{{ trigger.to_state.state }}"
The input_select was a workaround to aid in troubleshooting/ debugging.
The previous automation had some odd issues e.g. the max light_step (6) was never coming on and I couldn’t figure out a way to identify what’s not working (the logbooks would only show the automation being fired multiple times)
WIth the input_select as an intermediary variable, the intent was to be able to see what is happening behind the scenes.
Good to know the to_state construct… I thikn it will come in very handy for this and future automations
Unfortunately all the unified code constructs failed for one reason or the other.
Now I am sure the fault lies with me but the triggers and actions were behaving rather oddly esp during the day.
It’s monsoon season here and the skies keep getting overcast and clearing up causing multiple state changes in quick succession which would then lead to light flickering on and off throughout the house.
I have switched to the following code - which I suppose is a rather inefficient way to achieve the result - but this appears to be working reliably
- id: '1595336932627'
alias: Stepped Lights - Step 6
description: ''
trigger:
- above: 260
entity_id: sensor.terrace_analog_a0
for:
hours: 0
minutes: 1
seconds: 30
platform: numeric_state
condition:
- after: 09:00:00
before: '21:00:00'
condition: time
action:
- scene: scene.lights_step_6
- id: '1595336885470'
alias: Stepped lights - Step 5
description: ''
trigger:
- above: 240
below: 259
entity_id: sensor.terrace_analog_a0
for:
hours: 0
minutes: 1
seconds: 30
platform: numeric_state
condition:
- after: 09:00:00
before: '21:00:00'
condition: time
action:
- scene: scene.lights_step_5
- id: '1595337043692'
alias: Stepped Lights - Step 4
description: ''
trigger:
- above: 220
below: 239
entity_id: sensor.terrace_analog_a0
for:
hours: 0
minutes: 1
seconds: 30
platform: numeric_state
condition:
- after: 09:00:00
before: '21:00:00'
condition: time
action:
- scene: scene.lights_step_4
- id: '1595337389154'
alias: Stepped Lights - Step 3
description: ''
trigger:
- above: 200
below: 219
entity_id: sensor.terrace_analog_a0
for:
hours: 0
minutes: 1
seconds: 30
platform: numeric_state
condition:
- after: 09:00:00
before: '21:00:00'
condition: time
action:
- scene: scene.lights_step_3
- id: '1595337457454'
alias: Stepped Lights - Step 2
description: ''
trigger:
- above: 180
below: 199
entity_id: sensor.terrace_analog_a0
for:
hours: 0
minutes: 1
seconds: 30
platform: numeric_state
condition:
- after: 09:00:00
before: '21:00:00'
condition: time
action:
- scene: scene.lights_step_2
- id: '1595337564907'
alias: Stepped Lights - Step 1
description: ''
trigger:
- above: 160
below: 179
entity_id: sensor.terrace_analog_a0
for:
hours: 0
minutes: 1
seconds: 30
platform: numeric_state
condition:
- after: 09:00:00
before: '21:00:00'
condition: time
action:
- scene: scene.lights_step_1
- id: '1595337822101'
alias: Stepped Lights - Step 0
description: ''
trigger:
- below: 159
entity_id: sensor.terrace_analog_a0
for:
hours: 0
minutes: 1
seconds: 30
platform: numeric_state
condition:
- after: 09:00:00
before: '21:00:00'
condition: time
action:
- scene: scene.lights_step_0