If/then condition in automation

Hello there,
I’ve got following automation that works perfectly well apart from if/then condition. If i remove the condition and only leave one of the switches (ie switch.stairs_light_night) it works fine.

What is the best wat to do it with conditions? I.e. depending on the hour i need one or another switch to turn on. Is there any way to avoid two automation records?


    - id: '1536778262822'
      alias: Stairs lights on
      trigger:
      - entity_id: binary_sensor.motion_sensor_158d00023756b5
        from: 'off'
        platform: state
        to: 'on'
      condition:
      - below: '30'
        condition: numeric_state
        entity_id: sensor.illumination_158d00023756b5
      action:
      - data:
          entity_id: '{% if now().hour > 22 or now().hour < 6%} switch.stairs_light_night {% else %} switch.stairs {% endif %}'
        service: switch.turn_on

i didnt find elegant way to do it so trying to do achieve my goal with two almost same automations now.
This works:

- id: '1536778262822'
  alias: Stairs night lights on
  trigger:
  - entity_id: binary_sensor.motion_sensor_158d00023756b5
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - below: '30'
    condition: numeric_state
    entity_id: sensor.illumination_158d00023756b5
  - before: '07:00:00'
    condition: time
  action:
  - data:
      entity_id: switch.stairs_light_night
    service: switch.turn_on

but when i try to add condition: before: '22:00:00' it fails. I tried to play around with condition: or` with no success so far. How can i do it properly??

Thanks,
Alex

data_template, not data

Nope,
this doesnt change anything (i assume your suggestion was about my original question/config):

  - data_template:
      entity_id: '{% if now().hour > 22 or now().hour < 6%} switch.stairs_light_night {% else %} switch.stairs {% endif %}'

Yup, that should work fine. Any errors?

Needs to be a space after the 6 btw.

hmm, not sure. I’ve got this piece of automation for lights that works fine (there is no space after 6):

      color_name: '{% if (range(0,11) | random) <= 9 %}white{% else %}purple{% endif %}'

There isn’t a 6 in that one, there’s a 9 and it has a space after it.

oops
i copied wrong line, here it is what i meant:

      brightness: '{% if now().hour > 22 or now().hour < 6%} 1 {% else %} 255 {% endif %}'

That won’t work, the %} needs to be seen as one character.

The above works though.

Also i tried

  action:
  - data_template:
      entity_id: '{% if now().hour > 22 or now().hour < 6 %} switch.stairs_light_night {% else %} switch.stairs {% endif %}'
    service: switch.turn_on

and this didnt work. Config check passed fine but when i try to trigger nothing happens although both switches work fine…

Errors in the log?

This works in my template editor.

{% if now().hour > 22 or now().hour < 6 %}
switch.stairs_light_night
{% else %}
switch.stairs
{% endif %}

You need to have the service before its data_template.

  action:
  - service: switch.turn_on
    data_template:
      entity_id: '{% if now().hour > 22 or now().hour < 6 %} switch.stairs_light_night {% else %} switch.stairs {% endif %}'

Woohoo that worked, thanks a million @JeffLIrion !!

Alex