Whats wrong with this template

Not sure what is wrong with this

  action:
  - service: homeassistant.turn_off
    data_template:
      entity_id: >-
        {% if trigger.event.data.entity_id == 'timer.paul_just_arrived' %}
          input_boolean.back_door_opened_for_paul
        {% elif trigger.event.data.entity_id == 'timer.tracy_just_arrived' %}
          input_boolean.back_door_opened_for_tracy
        {% else %}
          input_boolean.back_door_opened_for_guests
        {% endif %}

I give up. What’s wrong with it?

2 Likes

Haha. I guess I should look closer at the rest of my automation. I figured tt was the template because I always struggle with them.

What’s happening when this automation triggers? Do you get an error, or just none of the input_boolean values are changed?

I’m not 100% sure but I think you don’t need the - after the > in the template:

action:
  - service: homeassistant.turn_off
    data_template:
      entity_id: >
        {% if trigger.event.data.entity_id == 'timer.paul_just_arrived' %}
          input_boolean.back_door_opened_for_paul
        {% elif trigger.event.data.entity_id == 'timer.tracy_just_arrived' %}
          input_boolean.back_door_opened_for_tracy
        {% else %}
          input_boolean.back_door_opened_for_guests
        {% endif %}

It was just a few extra spaces elsewhere in my automation. Posting for future use for others

- alias: 'Reset door lock boolean'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data: 
      entity_id:
        - timer.paul_just_arrived
        - timer.tracy_just_arrived
        - timer.guests_just_arrived
  action:
  - service: homeassistant.turn_off
    data_template:
      entity_id: >-
        {% if trigger.event.data.entity_id == 'timer.paul_just_arrived' %}
          input_boolean.back_door_opened_for_paul
        {% elif trigger.event.data.entity_id == 'timer.tracy_just_arrived' %}
          input_boolean.back_door_opened_for_tracy
        {% else %}
          input_boolean.back_door_opened_for_guests
        {% endif %}

So, is this the automation that works? If it is, I’m not sure I understand how it can work. I was just looking at the event trigger code, and I think to match the event trigger you defined, a timer.finished event would have to have been fired that contained event data of entity_id which contained a list of three timer entity_id’s, which I don’t think is possible. Are you sure this works?

Or, is that the problem - i.e., that this doesn’t work and you’re trying to figure out how to make it work?

1 Like

I thought it was working but definitely could be wrong. Will do some testing.

The goal was to turn off the input Boolean when the timer finisjed

I think, no matter what, you can’t have all three timers listed in one trigger. What I would do (assuming that your current automation indeed doesn’t work) is to simply remove the event_data parameter from the trigger. Then add a condition like this:

  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in ['timer.paul_just_arrived',
                                          'timer.tracy_just_arrived',
                                          'timer.guests_just_arrived'] }}
1 Like

Like this?

- alias: 'Reset door lock boolean'
  trigger:
  - platform: event
    event_type: timer.finished
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in ['timer.paul_just_arrived',
                                          'timer.tracy_just_arrived',
                                          'timer.guests_just_arrived'] }}
  action:
  - service: homeassistant.turn_off
    data_template:
      entity_id: >-
        {% if trigger.event.data.entity_id == 'timer.paul_just_arrived' %}
          input_boolean.back_door_opened_for_paul
        {% elif trigger.event.data.entity_id == 'timer.tracy_just_arrived' %}
          input_boolean.back_door_opened_for_tracy
        {% else %}
          input_boolean.back_door_opened_for_guests
        {% endif %}
1 Like

Yes.

FWIW, I would change the service to input_boolean.turn_off. homeassistant.turn_off will just turn around and call input_boolean.turn_off, so you might as well call it directly. It’s more efficient that way. The homeassistant services are really there when you have a mix of entities you want to change in one service call (e.g., turning on switches and lights at the same time.)

1 Like

good you mention this, I’ve been struggling a bit on that issue. Could the collected homeassistant.turn_on/off cause timing issues when the list of entities is rather large? I had all dedicated services before in several scripts, ran into some issues of non-execution, then turned to the homeassistant service. Works alright, but every now and then the system seems to break up, because on of the entities doesn’t respond and the whole service halts… Maybe calling these services individually would be better after all?
Is the efficiency you mention to be monitored somehow?

1 Like

Using a homeassistant service as opposed to the ultimate domain’s service (e.g., homeassistant.turn_on instead of light.turn_on) just adds a layer of indirection (and, hence, extra processing.) How large the list of entity_id’s is doesn’t have anything to do with it. That list is only processed by the ultimate domain’s service (e.g., light.turn_on.) If the corresponding domain’s service has an issue, then calling it indirectly via a homeassistant service isn’t going to change that.

If you’re interested, you can see the homeassistant service code here.

Regarding how to “monitor” the efficiency, you can see all services that are called in the HA log by searching for call_service. E.g., here’s an example of turning on a light via homeassistant.toggle. You can see that it just turns around and calls light.toggle:

2018-08-28 09:51:36 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=entity_id=light.master_bedroom_nightstand_lamp, service=toggle, domain=homeassistant>
2018-08-28 09:51:36 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=entity_id=['light.master_bedroom_nightstand_lamp'], service=toggle, domain=light>
2018-08-28 09:51:36 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]>
2018-08-28 09:51:36 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]>
2018-08-28 09:51:39 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state light.master_bedroom_nightstand_lamp=off; node_id=9, friendly_name=Master Bedroom Nightstand Lamp, value_id=72057594193739777, supported_features=33, value_instance=1, value_index=0 @ 2018-08-28T08:50:23.878734-05:00>, new_state=<state light.master_bedroom_nightstand_lamp=on; node_id=9, friendly_name=Master Bedroom Nightstand Lamp, value_id=72057594193739777, brightness=255, value_instance=1, supported_features=33, value_index=0 @ 2018-08-28T09:51:39.908774-05:00>, entity_id=light.master_bedroom_nightstand_lamp>

There isn’t all that much overhead, so it probably doesn’t matter too much. My point was, why bother using the homeassistant service if you’re changing entities that you know are all in the same domain, or you’re just changing one entity.

1 Like