If/else inside a script doesn't work

Hey,
I try to have a script that triggers one of the other scripts based on a ping sensor. It is an on/off script for my laptop. I used the same scripts in connection with the sensor in a switch which worked, but I really need one script that just knows if the laptop is on or off and triggers the right script to turn it on or off based on the binary ping sensor. At the moment it does nothing. Any idea?

laptop_scene:
  sequence:
  - service_template: >
      {% if is_state('binary_sensor.ping_laptop', 'off') %}
         service: script.turn_on
         entity_id: script.laptop_on_routine
      {% else %}
         service: script.turn_on
         entity_id: script.laptop_off_routine
      {% endif %}

You can’t make a template that gives multiple lines as an output in Jinja, it will always return only one field.

Also with scripts, you can use the script directly as the service, you don’t need service and entity_id. Also entity_id is part of data and not of the service.

This should work:

laptop_scene:
  sequence:
  - service_template: >
      {% if is_state('binary_sensor.ping_laptop', 'off') %}
         script.laptop_on_routine
      {% else %}
         script.laptop_off_routine
      {% endif %}
1 Like

Thank you! I tried your script and it somehow still doesn’t trigger one script or the other (did a restart of HA). I have all of my scripts in scripts.yaml and not in configuration.yaml (including this) does that make any difference? Maybe it can’t access one of the scripts or the sensor that way?

I edited my post afterwards but somehow I don’t see the edit anymore. The service: shouldn’t be there.

laptop_scene:
  sequence:
    - service_template: >
        {% if is_state('binary_sensor.ping_laptop', 'off') %}
           script.laptop_on_routine
        {% else %}
           script.laptop_off_routine
        {% endif %}

Anyway, I reread your original post and I think it would be easier to create a template switch something like this:

switch:
  - platform: template
    switches:
      laptop_switch:
        value_template: "{{ is_state('binary_sensor.ping_laptop', 'on') }}"
        turn_on:
          service: script.laptop_on_routine
        turn_off:
          service: script.laptop_off_routine

This will get the state of the switch from the ping sensor.

2 Likes

Thank you so much for help!! This works great.
And I totally agree with you on switches. I have a similar one running, but I have this app for my smartwatch that only supports “scenes” but has a really nice interface.

So I wanted a switch, but couldn’t use it without having two scenes for each state. Putting a script inside a scene that always turns on and leaving the rest to Home Assistant helps me to bypass that restriction.

Thanks again for your fast help. I really appreciate it! I am not a programmer and try to figure out the stuff on my own, so I am always happy to learn new things from others :slight_smile:

2 Likes

Glad that you got it to work :smiley: I’m also not a programmer, just happy when I’m able to help others :wink:

1 Like