Always surprises me when I go to do something in HA and realize that I can’t figure out how.
This time its lights, specifically making sure that they don’t get left on.
Until now I’ve simply been creating an automation for each light switch so that if it changes state from Off to On and when it’s 30 minutes after sunrise it’s starts a 15 minute wait and then changes the state of the switch to off.
This approach mostly works but it’s less than ideal.
First I’m having to create an automation for each device. How do I do it by Area, or list / group of devices, instead?
Second if a device is turned on too early there’s no state change for the automation to catch and it never fires. I could fix this by creating another automation that checks for it but then I’ll have even more of them to manage.
Third this doesn’t work very well if you want different things to happen on the weekends as opposed to during the weekday. For instance on a Saturday I may WANT that closet light to stay on longer because I’m putting away clothes.
It’d be really nice if I could program HA like this ‘On a weekday if you see any device on this list turn on 30 minutes after Sunrise I want you to turn whichever one(s) it was off again 15 minutes later.’.
I’m must be missing something here because surely HA can do this, right?
This is a great case for templates. Templates are Home Assistant’s way of doing dynamic actions or data.
When: whatever triggers you need for the lights - can be multiple triggers or one trigger with every light/switch listed, with a For clause. The list is "OR"ed so do however you want. You could use a group but then all devices would be changed together - which doesn’t sound like is what you want.
And if: Whatever conditions you want, time, day of week, etc.
Then do: The action is where it gets interesting add an action to execute “Generic turn off”, select any device (we’ll change that in a moment). The reason for the “Generic turn off” is that it works with devices in any domain (light or switch). Then drop into the YAML view and chcange the entity_id value to this template:
'{{ trigger.from_state.entity_id }}'
. The trigger is a variable that’s available inside the automation and can be used instead of specifying a static value somewhere. https://www.home-assistant.io/docs/automation/trigger/ for details on triggers.Here’s the complete automation, you could create a new empty automation, then copy and paste this in place and just change the trigger and conditions as you want.
alias: Action - Turn off lights 15min after turned on during day description: "" trigger: - platform: state entity_id: - switch.porch_light for: hours: 0 minutes: 15 seconds: 0 enabled: true to: "on" from: "off" condition: - condition: sun after: sunrise after_offset: "30" - condition: time after: "01:00:00" before: "23:00:00" weekday: - sun - mon - tue - wed - thu - fri - sat action: - service: homeassistant.turn_off metadata: {} data: {} target: entity_id: "{{ trigger.from_state.entity_id }}" mode: queued max: 10