Hi!
Played around with my code last night trying to make it simpler using templates. No luck, stopped working. Anyone that might help me in the right direction? I guess templates is the answer, but could not get it working.
- alias: "Kjokken - Panelovn - Av"
initial_state: "on"
trigger:
- platform: state
entity_id: input_boolean.kjokken_temperatur_over_20_grader
to: 'on'
for:
minutes: 1
- platform: state
entity_id: input_select.varmekabler_modus
to: 'Nattsenking'
- platform: state
entity_id: input_select.varmekabler_modus
to: 'Nattsenking fridag'
- platform: state
entity_id: input_select.varmekabler_modus
to: 'Ettermiddagsmodus'
- platform: state
entity_id: input_select.varmekabler_modus
to: 'Feriemodus'
- platform: numeric_state
entity_id: sensor.yr_temperature
above: 14
condition:
condition: or
conditions:
- condition: state
entity_id: input_boolean.kjokken_temperatur_over_20_grader
state: 'off'
- condition: state
entity_id: input_select.varmekabler_modus
state: 'Nattsenking'
- condition: state
entity_id: input_select.varmekabler_modus
state: 'Nattsenking fridag'
- condition: state
entity_id: input_select.varmekabler_modus
state: 'Feriemodus'
- condition: state
entity_id: input_select.varmekabler_modus
state: 'Ettermiddagsmodus'
action:
- service: climate.set_temperature
data:
entity_id: climate.kjokken
temperature: '10'
- delay: '00:00:10'
- service: switch.turn_off
entity_id: switch.kjokken_panelovn
@jparthum, changing the trigger the way you should would actually change the behavior of the automation. As written in the OP, it would trigger whenever the input_select changed to any of the specified values, even if it changed from one of them. The template trigger you wrote would only trigger if it changed to one of the specified values from any value except for one of the specified values.
And, FWIW, the template condition could be simplified by just having a single āorā expression inside {{ }}, kind of like you did in the template trigger (well, with the outside quote characters removed.)
Iām with @jazzyisj ā @christian1986, what are you trying to simplify? If you just want it to be written in less lines, then yes, a template condition might do that, but it really wouldnāt simplify anything.
FWIW, the condition section of the automation, to me, seems a bit suspect. I donāt know exactly what the automation is supposed to do, but the conditions donāt seem right.
Hereās one way to reduce the size of the condition section:
condition:
condition: or
conditions:
- condition: state
entity_id: input_boolean.kjokken_temperatur_over_20_grader
state: 'off'
- condition: template
value_template: >-
{% set modus = ['Nattsenking', 'Nattsenking fridag', 'Feriemodus', 'Ettermiddagsmodus'] %}
{{ states('input_select.varmekabler_modus') in modus }}
EDIT
Reduced even further using pnbrucknerās refinement.
Replaced: {{ true if states('input_select.varmekabler_modus') in modus else false }}
with: {{ states('input_select.varmekabler_modus') in modus }}
If you want to reduce it even further:
value_template: "{{ states('input_select.varmekabler_modus') in ['Nattsenking', 'Nattsenking fridag', 'Feriemodus', 'Ettermiddagsmodus'] }}"
Oops! I totally disregarded the fact that itās a Trigger not a Condition.
I was providing a couple of different methods since he seems to have a good grasp of the logic and was just having trouble with syntax.
I personally preferred (before moving to Node-RED) having larger lists of variables in ācontainedā in templates. For me it was easier to quickly locate and differentiate components and states from the various functions in large .yaml files. It also seems like it would help performance, but I have no compelling evidence.
in is just a test to see if something (in this case a string, which is the current state of input_select.varmekabler_modus) is in a container (in this case modus, which is just a list variable that contains a bunch of strings created in the previous statement.)
If Iām understanding correctly, this assumes no other states exist in the container (input_select.varmekabler_modus) beyond whatās listed in the condition?
I wouldnāt use that terminology. input_select.varmekabler_modus is an entity, and like all entities in HA, itās state (if it has one ā i.e., if itās represented in the state machine) is a string.
The template @123 wrote is simply trying to be equivalent to the conditions in the OP. And if the state is not one of the values, then it will be false.