I’m trying to get an automation working, where a specific script should be turned on depending on which binary sensor triggered the automation.
For some reason I can’t get the automation to fire any other than the first script in my data_template action.
Your if and else if statements are doing nothing for you. It will hit the first if statement every time and script.1586436129877 will always be executed. This is because the way you wrote the if statement. {% if "trigger.to_state.entity_id", "binary_sensor.stue_altandoer_sensor" %}
Will always be True because you’re saying if this string is not empty, and then you provide it 2 non-empty strings.
You’re not actually checking the trigger object. You have the correct name for it, but then you place quotes around it, turning it into a string. "trigger.to_state.entity_id" is a string trigger.to_state.entity_id is the trigger object. Notice the lack of quotes?
You need to verify that the entity_id equals the sensor you care about. This is done with a double equal sign. ==
You writing this in a way that is extremely hard to read and debug. Do yourself a favor and use multiline templating. This is indicated with a > after the field in question.
You don’t have a fallback plan. I.e. an else statement. What happens if your entity_id doesn’t match anything? This is typically controlled by your trigger, but we can’t see it. It would be best to have a fallback.
Also, if you really want to make this expandable with little to no overhead when adding a new binary sensor, you can use a dictionary to get the job done.
Thank you so much for the answers, especially @petro for the very extensive one! Learning new stuff every day.
I’m quite new to code, so this community really is a big help, even thou I still have trouble wrapping my head around a lot of it.
The reason for not having a fallback in this automation was that I specify exactly the possible entity_id’s, so nothing else than the ones listed, should be a possibility.
I’ve changed format like you recommended. The automation works now, but I still get a few errors:
Notify dør åben i 20 min: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data['data']
Notify dør åben i 20 min: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']
Notify event sluk varme: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']
Here is the complete set of automations. Basically it’s supposed to notify me or my wife, if a door or window has been open for more than 20 minutes, then give us the option to either turn off the heating in the specific room (1. action in the notification) or leave it alone (2. action).
So the first automation fires a script for one of the four rooms, which then handles sending the notification, if specific criteria are met.
The second automation handles the selected action of the notification.
My problem at the moment is that I can’t seem to understand, how to make it act on the event of the notification. This has worked before, but only for one room. Should the formatting be different in this case?
- id: '1586125446593'
alias: Notify dør åben i 20 min
description: ''
trigger:
- entity_id: binary_sensor.spisestue_altandoer_sensor
for: 00:20:00
platform: state
to: 'on'
- entity_id: binary_sensor.stue_altandoer_sensor
for: 00:20:00
platform: state
to: 'on'
- entity_id: binary_sensor.kontor_altandoer_sensor
for: 00:20:00
platform: state
to: 'on'
- entity_id: binary_sensor.sovevaerelse_altandoer_sensor
for: 00:20:00
platform: state
to: 'on'
condition:
- condition: template
value_template: '{{ (state_attr(''weather.hjem'',''temperature'')) < 20 }}'
action:
- data_template:
entity_id: >
{% if trigger.to_state.entity_id == "binary_sensor.stue_altandoer_sensor" %}
script.1586436129877
{% elif trigger.to_state.entity_id == "binary_sensor.spisestue_altandoer_sensor" %}
script.1586436129878
{% elif trigger.to_state.entity_id == "binary_sensor.sovevaerelse_altandoer_sensor" %}
script.1586436129879
{% elif trigger.to_state.entity_id == "binary_sensor.kontor_altandoer_sensor" %}
script.1586436129880
{% endif %}
service: script.turn_on
The Automation Editor is well known for not only having limitations, it stores its data in YAML format that is perfectly legal but with compromised legibility. The primary complaint is that it sorts key names alphabetically thereby stripping it of its original logical order. Other complaints are its handling of quotation marks and newlines.
Whatever you do, never use it to edit and save an automation you had created manually. Not only will it reformat it (the way I’ve described and he way you’ve experienced), it will remove comments you had included (because the Automation Editor doesn’t support comments).
Thank you for you description.
This sounds like a very good idea! Just to be sure to understand it correctly, you have an individual xyz.yaml for each manually created automation in the automations/ folder?
Another method is "don’t list your automations with " - id: " just with " - alias: " and the editor should steer clear of them
Edit: DON’T do this. See : -
The ui will list all automations but simply won’t allow you to edit ones ‘without’ an id
It’s quick and dirty but Taras’s suggestion is ‘more’ flexible as you get the best of both worlds.
True but if manually-created automations are stored in automations.yaml that’s the file used by Automation Editor to store its automations. When it saves its automations, it strips all comments found in the file and sorts options alphabetically. I learned this, as they say, the ‘hard way’.
I could if I wanted to but I don’t. Each file contains several automations, all related to a specific purpose. For example, ‘pool.yaml’ contains all automations that control the pool pump and lights.
I posted my newfound knowledge (which I could have gained, with less frustration, by simply reading the documentation) in this topic: School of hard Knocks: the Automation Editor. A classic case of “RTFM”.