How to place condition into a service of a script?

Hi,

In the following script I would like to turn on the living_room_lamp only after sunset. I believe if I would change the order and first turn off the avr/projector, after place the condition and after the turn_on lamp then the script would stop if the condition is not right (the sun is up), but I need this order since I do not want to be in a dark room for a moment… So probably the question is how to put a condition inside of the “-service” part in a script.

Thank you in advance!

turn_off_tv:
  sequence:
    - service: switch.turn_on
      entity_id: switch.living_room_lamp
    - delay:
      seconds: 30
    - service: script.turn_on
      entity_id:
        - script.avr_turn_off
        - script.projector_turn_off
        - script.projector_screen_up

Sorry, misread you question - let me fix.

Something like this

turn_off_tv:
  sequence:
    - service_template: >
        {% if is_state('sun.sun', 'below_horizon') %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
      entity_id: switch.living_room_lamp
    - delay:
      seconds: 30
    - service: script.turn_on
      entity_id:
        - script.avr_turn_off
        - script.projector_turn_off
        - script.projector_screen_up
1 Like

Thanks a lot! I am not sure why did not I think for templates…

1 Like

For some reason it is leading to an invalid configuration and I do not see what can be wrong. Any help would be much appreciated!

This is the code:

turn_off_tv:
  sequence:
    - service_template: >
        {% if is_state('sun.sun', 'below_horizon') %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% end if %}
      entity_id: switch.living_room_lamp
    - delay:
        seconds: 3
    - service: script.turn_on
      entity_id:
        - script.avr_turn_off
    - delay:
        milliseconds: 200
    - service: script.turn_on
      entity_id:
        - script.projector_turn_off
    - delay:
        milliseconds: 200
    - service: script.turn_on
      entity_id:
        - script.projector_screen_up

And this is the relevant part of the config validation:

turn_off_tv: [source /home/tiszavolgyi/.homeassistant/scripts/livingroom.yaml:40]
      sequence: [source /home/tiszavolgyi/.homeassistant/scripts/livingroom.yaml:41]
        - entity_id: switch.living_room_lamp
          service_template: {% if is_state('sun.sun', 'below_horizon') %}
  switch.turn_on
{% else %}
  switch.turn_off
{% end if %}

        - delay: [source /home/tiszavolgyi/.homeassistant/scripts/livingroom.yaml:49]
            seconds: 3
        - entity_id: [source /home/tiszavolgyi/.homeassistant/scripts/livingroom.yaml:52]
            - script.avr_turn_off
          service: script.turn_on

My fault, I got owned by autocorrect.

It’s probably because of this

{% end if %}

Should be

{% endif %}

OMG, thanks, how could I be so lame did not recognize it… Sorry for that and thank you so much!

1 Like

Haha, no problem. It took me a minute to see it as well!