Using a single automation for multiple devices

I have recently done a full reinstall of my 3 year old HA system, and am running HassOS in a VM on Proxmox. I love doing the automations in the GUI, with the possibility of editing the files/entries manually as necessary.

Over the past week, I have been thinking how to set up a multi-in/multi-out automation. I have several uses for it, but let me explain the most simple one, based on a timer:

If I leave the fan on when I leave the bathroom, I want the fan to turn off after 15 minutes. This is a simple automation with the ‘state for…’ timer.

However, I have 5 bathroom fans in my house, each one connected to a smart switch. This means I have to create 5 identical automations, one for each switch.

How can I create a single automation for the following:

  • Trigger a ‘state for…’ any of the 5 devices (this is the simple part)
  • ‘Retrieve’ the device that triggered the automation (for example, with the template {{ trigger.to_state.attributes.friendly_name }})
  • Use the retrieved value to run the ‘switch.turn_off’ on that specific device
    – Without using a helper variable that may get overwritten when 2 devices happen to trigger at the same time

Can I somehow dynamically assign the entity for use in the action? Something along the lines of:

data: {}
service: switch.turn_off
entity_id: '{{ trigger.to_state.entity_id }}'
1 Like

I came here to ask the same question, but you beat me by a few hours. I wanted to do this for a long time, but right now I am very motivated since I am also rebuilding a fresh install of HA after the previous long-running one died of old age.

The best option I found so far is Schedy. Unfortunately it requires you to learn another flavor of YAML commands, but it looks like it might be able to do what we want.

Note that my old HA installation managed lights in many rooms on a scheduled basis, so I am actually interested in the scheduling functionality as well. For just combining automations, it may be overkill.

Slightly different topic: are you running one of the pre-built images on Proxmox? My previous install was supervised HA running on generic Linux in a Proxmox VM, but that config is no longer supported. I found that the latest pre-built images run once in Proxmox, but fail to boot again after being shut down for the first time, so I had to move to another VM host temporarily.

Probably, not the answer you were looking for , but in your example you could just use 5 template switches matching the smart switches which trigger entity timers and the fan when turned on. Then one simple automation that trigger when one of the timers has finished and turns the the template switch off. If you name the timers to match the template switches you can use a simple substitution on the value template in the automation.

The above paragraph is much simpler to write than trying to work out the code :slight_smile: , but I find moving the logic away from the automatons , simplifies things.

I have used input_selects as variables , so your automatons can hold persistent data if that helps

1 Like

@quentinv - That’s funny, I am in a similar situation. About 3 years ago, I built Hassio on a Docker VM in Proxmox (unsupported) and have been fiddling around with HA since them. Last week, I decided to rebuild using hassova, to get a fully supported install, because I am getting more serious with home automation.

I was going to do a RPi install, but found that there are proper VM images available. I could not get the KVM image to work properly, so I followed the instructions in this post:

It has been working perfectly since the install. Let me know if you have any issues, maybe I can help (I have been a PMX user for about 10 years, both personally and professionally).

I’ll take a look at Schedy (have not heard of it), but I would like to keep the install clean and as much ‘native’ as possible. I am hoping to do it in HA itself.

Man, that’s a cool, creative solution. I am going to try this tonight or tomorrow. Will let you know if this works for me! Thanks!

I can’t work out what your question is, because the last three lines of your post seem to answer the question that the rest of it seems to be asking :man_shrugging:

For completeness

  platform: state 
    - FAN_1
    - FAN_2
    - ETC
  to: 'on' 
    minutes: 15
  service: switch.turn_off
    entity_id: "{{ trigger.to_state.entity_id }}"

OMG - I feel so dumb now… :smiley: When I was playing around with the templates yesterday, it would not even let me save the automation when I tried it. I figured that I was using an illegal syntax, or something like that.

Looking at your example, which does EXACTLY what I need, I realized that my indentation was off: I was missing the double-space in front of entity_id and the data_template header…

Marking this one as solved! Thanks!!

1 Like

So after seeing the solution, I realized that I need a bit more than was originally asked. This works great for turning off devices, but in my case, the device that triggers the automation (motion sensor) is different from the controlled device (switch). Is there a way to create an entity-to-entity ‘map’ that would allow a generic automation like the above to turn on multiple entities triggered by multiple sensors?

@digibeet - I did follow the guide you linked to and I am even running on one of the tested machines (Dell Optiplex SFF 990). As I mentioned, the image boots up, installs HA, and works perfectly - until I shut it down. Thereafter it never boots again, failing with a memory allocation error in the UEFI boot stage.

The image runs and reboots fine under VMware and Synology VM manager, which is where I am running it for now until I can figure out the cause of the Proxmox issue.

Can be done if you strategically name your entities

binary_sensor.kitchen_motion =
binary_sensor.bathroom_motion = light.bathroom

  - platform: state 
    entity_id: &motion
      - binary_sensor.kitchen_motion
      - binary_sensor.bathroom_motion
      - ETC
    to: 'off' 
      minutes: 15
  - platform: state 
    entity_id: *motion
    to: 'on' 
  service_template: "light.turn_{{ trigger.to_state.state }}"
    entity_id: >
      {{ trigger.to_state.entity_id
          |replace('binary_sensor', 'light') 
          |replace('_motion', '') }}

I’ve taken the liberty of adding the turn on part to the same automation (turns the light on instantly when motion, turns it off 15 minutes after motion stops).


@anon43302295 Oh wow, that completed the puzzle for me. I did not know about the ‘replace’ functionality, but that would absolutely work for my use case!

1 Like

Hey @anon43302295,

Thanks for your solution.
Could you explain the &motion and *motion values for entity_id?