Help simplifying automation - Template?

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

What are you trying to simplify? Where do you think you need a template here? Everything looks pretty straight up to me.

Try thisā€¦

- 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: template
    value_template: >
      "{{ is_state('input_select.varmekabler_modus', 'Nattsenking') 
      or is_state('input_select.varmekabler_modus', 'Nattsenking fridag') 
      or is_state('input_select.varmekabler_modus', 'Ettermiddagsmodus') 
      or is_state('input_select.varmekabler_modus', 'Feriemodus') }}"
  - platform: numeric_state
    entity_id: sensor.yr_temperature
    above: 14
  - condition: template
    value_template: >
        {% if is_state("input_boolean.kjokken_temperatur_over_20_grader", "off") %}
          true
        {%-elif is_state("input_select.varmekabler_modus", "Nattsenking") %}
          true
        {%-elif is_state("input_select.varmekabler_modus", "Nattsenking fridag") %}
          true
        {%-elif is_state("input_select.varmekabler_modus", "Feriemodus") %}
          true
        {%-elif is_state("input_select.varmekabler_modus", "Ettermiddagsmodus") %}
          true
        {% else %}
          false
        {% endif %}
  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

EDIT: Spacing was off.
EDIT 2: Erroneous false. :relaxed:

@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.

1 Like

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'] }}"
2 Likes

Or even:

          {{ states('input_select.varmekabler_modus') in modus }}
2 Likes

Oops! I totally disregarded the fact that itā€™s a Trigger not a Condition. :flushed:

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.

Thanks for the advice. :slight_smile:

?

I still have a lot learn. :sweat_smile:

With two flicks of your katana youā€™ve trimmed the template to its essentials. :wink:

1 Like

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.)

1 Like

Ah, I gotcha. I thought modus was a command! :laughing:

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.

1 Like

Oh I see now, the template creates the container and populates it. Very concise. :sunglasses: