I struggle a bit with setting up a bunch of automations.
What I have:
6 rooms
6x binary_sensor.[room] (window contact)
6x climate.[room] (heating)
“room” is the individual name of the room, like “living_room”, “bedroom”, etc. Reading each contact works, setting temperature/mode for each heating works.
Now I like to set up an automation, where the heating switches off, when the window in the room is opened and switch the heating back on again, when the window is closed.
Actually, my only idea how to implement this is 12 individual automations, two for each room (open/close) in this style:
- action:
- data:
entity_id: climate.living_room
operation_mode: lowering
service: climate.set_operation_mode
alias: Window Living Room open
condition: []
id: '1510611532639'
trigger:
- entity_id: binary_sensor.window_contact_living_room
from: closed
platform: state
to: open
- action:
- data:
entity_id: climate.living_room
operation_mode: comfort
service: climate.set_operation_mode
alias: Window Living Room closed
condition: []
id: '1510619252639'
trigger:
- entity_id: binary_sensor.window_contact_living_room
from: open
platform: state
to: closed
Now the question: is there a way to have this automation more “dynamic”, so that there are only two automation (one for open, one for close), which use the state change of any of these six binary_sensors to trigger the corresponding action of the corresponding climate component?
So having no way to actually test this, I came up with something you could try and see if it works (I’ll honestly be amazed if it does ):
You’d have to make 6 triggers, one for each window/climate pair, and make sure the pairs are named the same. Good luck!
trigger:
- platform: state
entity_id: binary_sensor.living_room
- platform: state
entity_id: binary_sensor.[next_room]
action:
service: climate.set_operation_mode
data_template:
entity_id: climate.{{trigger.entity_id.split('.')[1]}}
operation_mode: '{% if is_state('trigger.to_state.state', 'open') %} lowering {%else%} comfort {%endif%}'
This give me an idea of how to reduce my 60 automation into one. I put each entity that I want to monitor in its separate group. I then automate the visiblity of that group based on if the sensor is on or off. I had this before
automation show_switch_family_light:
trigger:
platform: state
entity_id: light.family_light
to: 'on'
action:
service: group.set_visibility
entity_id: group.family_light_alert
data:
visible: True
automation hide_switch_family_light:
trigger:
platform: state
entity_id: light.family_light
to: 'off'
action:
service: group.set_visibility
entity_id: group.family_light_alert
data:
visible: False
I will give it a try, when I am back home. But on the first glace, it looks as it could work . Of course this requires the entities to be identical, but in my case, this is totally fine (desired).
So, some time has passed and I did some tests. I wanted to use the same idea/style as proposed by @brg468 to move my window blinds up/down depending of the state of the corresponding widow sensor.
Sadly, I don’t get it running as described above. Here is a part of my automation code:
trigger:
- entity_id: binary_sensor.living_room
from: open
platform: state
to: closed
- entity_id: binary_sensor.dinning_room
from: open
platform: state
to: closed
condition:
- condition: numeric_state
entity_id: cover.{{trigger.entity_id.split('.')[1]}}
value_template: float(states.cover.{{trigger.entity_id.split('.')[1]}}.attributes.current_position)
below: '40'
The HA logs say:
Invalid config for [automation]: Entity ID cover.{{trigger.entity_id.split('.')[1]}} is an invalid entity id for dictionary value @ data['condition'][0]['entity_id']. Got None.
Well, the reason why I don’t do this in the action-part is, that it seems like a condition to me . “Do something, if the cover has state xxx!”, so the cover state is a condition, isn’t it?
I also tried your suggestion, but that fails, too. I don’t remember the reasen ATM, but I can check it later.
Actually I tried to implement the functionality described above in a much less complicated way: just one automation for each window and action.
The code seems to be “correct” in terms of the syntax. But the automation does still not work. It just does not get triggered, when I open the window.
And even if I trigger the automation manually, the input_number does not get the actual cover position and the cover does not move to the position defined by another slider.
So basically, nothing works . I gonna try some more things by myself. But if I don’t get it working, I need to ask more n00b-questions .
What does the dev-tools -> template give for the template?
Also this look incorrect to me…
trigger:
- entity_id: binary_sensor.living_room
from: open
platform: state
to: closed
- entity_id: binary_sensor.dinning_room
from: open
platform: state
to: closed
Shouldn’t it be …
trigger:
- platform: state
entity_id: binary_sensor.living_room
to: 'closed'
- platform: state
entity_id: binary_sensor.dinning_room
to: 'closed'
I think you’re missing the quotes in your value_template. Also, I’d convert to float at the end. Finally, you may need to not have quotes for the number since you don’t want it to be a string. Try this…
trigger:
- platform: state
entity_id: binary_sensor.living_room
from: open
to: closed
- platform: state
entity_id: binary_sensor.dinning_room
from: open
to: closed
condition:
- condition: numeric_state
entity_id: cover.{{trigger.entity_id.split('.')[1]}}
value_template: "{{ states.cover.{{trigger.entity_id.split('.')[1]}}.attributes.current_position | float }}"
below: 40
Thanks a lot, guys! In the meantime, I figured out a few things and found, that it was basically my condition-part, which was wrong. So the trigger actually works.
I am not at my PC atm, but I will try your suggestions soon and report back.
I am relatively sure, that I can manage to get it working so that the threshold/target position can be defined by another input_number slider component. Also the counter-automation to close blinds when window gets closed shouldn’t be a problem now.
But: I couldn’t get it working the “dynamic” way, where the corresponding blinds are moved depending on the window contact that triggered the automation. for first tests, I only modified the condition to be “dynamic”. Means THIS is NOT working:
Invalid config for [automation]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['condition'][0]['value_template']. Got none.
Where is the problem here (except that one that is sitting in front of my computer )?
@Jojo-A Are you using the Automation Editor? For some reason, the sequence of events is messed up. The reason I ask is, ever since it was released, I’ve seen more mixed up code on the forum and wondering what’s the reason for the sudden change. I got used to writing it manually so never bothered with the editor. Anyways, try this…it should work with the Trigger>Condition>Action sequence with proper spacing as per the documentation. This should work for you…
@Jer78
Thank you, I’ll give it a try as soon as I am back home. But why do you think your code should fix my problem? The error above seems not to be caused be wrong code order. And in your code, the syntax of the faulty part is identical to mine .
Anyway, I’ll give it a try.
Yes, am am using the Automation editor. You are right, this thing seems to smash the code around in random order. I think the “idea” of the editor is great, but atm, it lags of fundamental things. For example you can not define the logic combination of triggers/conditions (AND, OR). Maybe I switch over to the manual automation creation. I also like to have a “clean” code style…
Actually you are right. Numeric state condition can’t take a template. So what you’d need to do is first make a template sensor with the value_template and then use that new sensor as the numeric state condition.
Could this be a good potential candidate for a python script? You could create a dictionary that has sensor names as keys, and the corresponding heater for each value. You could then have an automation that is triggered by a state change on the sensors, and calls the python script.
@Jer78
well, actually you are not right that I am right . According to this page: https://home-assistant.io/docs/scripts/conditions/ numeric_state CAN have a value_template.
I am relatively sure, that I just have syntax errors with missing/wrong brackets, quotes, double-quotes, whatever.
Also it would be interesting to know, if the proposition of @brg468 (usage of {{trigger.entity_id.split(’.’)[1]}} ) would work at all. Until now I had no chance to verify that, because the second window is not ready yet. Maybe I try that by firing the other window events manually…
@Dolores
of course this might be another solution. But actually I am still too new in this HA stuff and have no experiences in Python at all. Please understand, when I don’t want to change the actual configuration, as I am happy, that it kind of “works” as it is atm