Service_template with data (HA not very flexible!)

I have an automation which turns off a light at a certain time but I want it only to do so if no movement has been detected. If movement is detected I want to call script to extend the off time by 4 minutes.

I do it like this, or at least I thought I could but the home_asisstant.turn_off doesn’t like the extra data. Why doesn’t it just ignore it?

Is there a way around this? How can I call different scripts (or services) based on a condition if they need different data?

    action:     
      - service_template: >
          {% if is_state('binary_sensor.pir_hall', 'off') %}
            homeassistant.turn_off
          {% else %}
            script.set_new_light_off_time
          {% endif %}
        entity_id: switch.hall_light
        data:
          room: hall
          extension: 4

Not certain it will work, but maybe try something like this?

action:     
  - service_template: >
      {% if is_state('binary_sensor.pir_hall', 'off') %}
        homeassistant.turn_off
      {% else %}
        script.set_new_light_off_time
      {% endif %}
    data_template: >
      {% if is_state('binary_sensor.pir_hall', 'off') %}
        entity_id: switch.hall_light
      {% else %}
        room: hall
        extension: 4
      {% endif %}
1 Like

That looks decidedly familiar now!
I wonder if I might have actually used this method before and just completely forgotten about it.

Thanks, I appreciate the help and I’ll give it a try.

I don’t think that will work because the last “else” statement contains more than one line.

there might be a way to do that with a single line and comma’s but I don’t know enough about that to tell you how it needs done. I just remember seeing it done once a while back.

@finity I think you’re right.

I found this thread [Service_template & data_template within 1 automation] which has some overlap but I quote it here as brazen attempt to bring in one of the templating big guns @petro

Sorry, out of town. Those templates won’t work. Jinja can only output 1 result. It can do multiple lines but the yaml will see it as “one output”. So you won’t be able to assign 2 attributes in 1 template. At that point, you’re better off making a script. I can’t really create a template cause I’m on my phone. But the general idea is 2 scripts, one does one action. The other does the other action. Then you programatically call the scripts. Pass all the variables you want because scripts will ignore things that the script doesn’t use. You won’t be able to use the entity_id attribute though as that is used by the service call for turning on a script.

1 Like

Thanks and no problem about the delay in responding!

I have already reworked everything so that I am doing pretty much what you suggested.

In fact I have decided that service_template (rather like wait_template but that is discussed elsewhere :wink: ) should be used with caution and only in the very simplest of circumstances as it tends to promises more than it can deliver(*). Breaking things up into even smaller scripts and using condition seems to be the best way to do these decision based flows.

I do wish that a script with a service_template with no satisfying condition would just continue processing and that a service could receive ‘unnecessary’ data.


(*)Improved documentation on both of these regarding how they actually work at a more internal level wouldn’t go amiss and would remove the expectation that they can offer more than they do.