Dynamic Automation for multiple components?

Dear community,

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?

Thanks and best regards!
Jojo

1 Like

Hi,

I think it has not become clear, what my question was about. Or there is just no “dynamic” solution :wink: .

So what I basically think about/like to do is something like that:

- action:
  - data:
      entity_id: climate.[room_to_set_climate_state]
      operation_mode: lowering
    service: climate.set_operation_mode
  alias: Window open
  condition: []
  id: '1510611063639'
  trigger:
  - entity_id: binary_sensor.[sensor_that_has_changed_its_state]
    from: closed
    platform: state
    to: open
- action:
  - data:
      entity_id: climate.[room_to_set_climate_state]
      operation_mode: comfort
    service: climate.set_operation_mode
  alias: Window closed
  condition: []
  id: '1510619232739'
  trigger:
  - entity_id: binary_sensor.[sensor_that_has_changed_its_state]
    from: open
    platform: state
    to: closed

If there is way to get it working this way, I think I will first have to logically “connect” sensors and rooms. In pseudocode:

if(sensor_that_has_changed_its_state == binary_sensor.living_room)
then (room_to_set_climate_state = climate.living_room)

Then I like to call my automation with both parameters “sensor_that_has_changed_its_state” and “room_to_set_climate_state”.

Could this be done maybe with templates or scripts?

Thanks a lot,
cheers!

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 :slight_smile: ):
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%}'
1 Like

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 think i can rewrite all them like this?

automation visibility_entity_group:
  trigger:
    - platform: state
      entity_id: light.family_light
    - platform: state
      entity_id: light. [next entitiy}
  action:
    service: group.set_visibility
    entity_id: group.{{trigger.entity_id.split('.')[1]}}
    data_template:
      visible: '{% if is_state('trigger.to_state.state', 'on') %} True {%else%} False {%endif%}'

Give it a shot! Like I said I haven’t tried this so I could be missing something, but in theory it should work like you have it.

Hello and thanks a lot both of you!

I will give it a try, when I am back home. But on the first glace, it looks as it could work :slight_smile: . Of course this requires the entities to be identical, but in my case, this is totally fine (desired).

I will report back my results.

Thanks!

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.

Any idea about that?

Thanks!

You probably can’t do that in a condition, try to do it in the action instead?

Or try it like this…

  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<40)

The value_template may need to be a data_template

@keithh666
Thanks a lot for your reply!

Well, the reason why I don’t do this in the action-part is, that it seems like a condition to me :slight_smile: . “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 :thinking: . I gonna try some more things by myself. But if I don’t get it working, I need to ask more n00b-questions :wink: .

Greetings!

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'

@Jojo-A

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

spacing might be wrong

image

@RobDYI you are right. It got messed up when I pasted. I edited it above

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.

Thanks again!

Alright, here is what I’ve got, this automation is working, means:

  • trigger is working (explicit window gets opened)
  • condition is working (explicit cover/blind is lower than constant value threshold)
  • actions are working (store actual position in explicit input_number component and move explicit cover to a defined position

Looks like this:

- action:
  - data_template:
      entity_id: input_number.dinning_room
      value: "{{states.cover.dinning_room.attributes.current_position | float }}"
    service: input_number.set_value
  - data:
      entity_id: cover.dinning_room
      position: '40'
    service: cover.set_cover_position
  id: '834611532639'
  alias: "Open blinds when window gets opened"
  trigger:
  - entity_id: binary_sensor.dinning_room
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: numeric_state
    below: 40
    entity_id: cover.dinning_room
    value_template: "{{states.cover.dinning_room.attributes.current_position | float }}"

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:

- action:
  - data_template:
      entity_id: input_number.dinning_room
      value: "{{states.cover.dinning_room.attributes.current_position | float }}"
    service: input_number.set_value
  - data:
      entity_id: cover.dinning_room
      position: '40'
    service: cover.set_cover_position
  id: '834611532639'
  alias: "Open blinds when window gets opened"
  trigger:
  - entity_id: binary_sensor.dinning_room
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: numeric_state
    below: 40
    entity_id: cover.dinning_room
    value_template: "{{states.cover.{{trigger.entity_id.split('.')[1]}}.attributes.current_position | float }}"

The HA log say:

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 :wink: )?

Thanks and greetings

@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…

  - id: '834611532639'
    alias: "Open blinds when window gets opened"
    trigger:
      - platform: state
        entity_id: binary_sensor.dinning_room
        from: 'off'
        to: 'on'
    condition:
      - condition: numeric_state
        entity_id: cover.dinning_room
        value_template: "{{states.cover.{{trigger.entity_id.split('.')[1]}}.attributes.current_position | float }}"
          below: 40
    action:
      - service: input_number.set_value
        entity_id: input_number.dinning_room
        data_template:
          value: "{{states.cover.dinning_room.attributes.current_position | float }}"
      - service: cover.set_cover_position
        entity_id: cover.dinning_room
        data:
          position: 40

@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 :thinking: .
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…

Greetings

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 :face_with_raised_eyebrow: . 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 :blush:

Greetings!