I have an automation to automatically turn off heated towel rails after 2 hours of being on. I’ve followed the same format for two towel rails. It works for the Ensuite, but not for the Bathroom, even though I’m using the same trigger and action rules.
Looks ok to me - other than using the abominable device actions and triggers. Try to avoid them and use service calls and state or numeric state triggers when you can. You’ll be thankful you did if you ever have to replace a device.
Did you do a config check and reload automations after making the changes (I think this happens automatically when using the automation editor, not really sure, I don’t use it).
What does the automation trace show after you attempt to trigger the ensuite towel rail?
With device triggers, there’s a key missing piece of information here: are those two towel rail switches created by the same integration for the same type of device?
Device triggers aren’t universal. Just because they are both switches doesn’t mean both have a turned_on type of device trigger. This is why the documentation strongly recommends setting those up from the UI. The set of options available for type is not documented and not validated by config check since it expects you picked an option in the dropdown in the UI. So if you try and do it from YAML you’ll just be guessing.
If you really want to manage this automation in YAML then do what the documentation suggests and use the UI then copy and paste the YAML. Or alternatively switch to this:
If you really want to manage this automation in YAML then do what the documentation suggests and use the UI then copy and paste the YAML.
Thanks for your response. I actually created and am managing this automation using the UI. I only copied the YAML here as it was easier to share. So I’ve selected the settings populated in the UI. Both are the exact same type of device (Shelly Plus 1PM), with the same settings tied to them.
I have a feeling I may need to just split it back out into two separate automations.
I’m trying to use the Automation UI in Home Assistant so it should technically work whether it’s a Device trigger or Call Service no? I might swap them to be a call service to see whether it works that way.
What does the automation trace show after you attempt to trigger the ensuite towel rail?
Ensuite is working, so the debug (show trace) looks fine. For the Bathroom one, the trigger never switches to “true” so it just doesn’t run.
Will try swap to “Call Service” instead of Device and see if that helps. Worst case, I’ll just separate them out.
Yea it does sound like it should, it can just be a bit harder for those of us on the forum to help for the reasons I laid out above. We can’t really tell you for sure if your YAML is right because we have no clue what goes in the type field in your actions and triggers.
That being said it does look right and if you’re using the UI it should be setting all the type and entity_id fields correctly…
One other thing to note that might be throwing this off, 2 hours is quite a while. Keep in mind that as the documentation notesfor won’t survive restarts or automation reloads. So if you’re trying to test this by waiting 2 hours but also doing other things in HA that involve restarting or reloading automations your test isn’t going to work for that reason.
Yes to both. State changes in Dev Tools and the ID is correct. Separate to this, I’ve actually got a template sensor that I created to tell me how long the heated towel rail has been on and that works correctly using the State.
Yeah I had thought of this too, but as I mentioned haven’t had issues with the other one working and have even made sure to not have any Home Assistant restarts happen during the time just to make sure it’s not that causing it. But I have a template sensor that I created to tell me how long the device has been on for. This is working as intended and if I restart Home Assistant, this would also restart the counter. That’s at least how I can tell that the device state has been on for 2 hours and hence the automation should’ve run.
I’m baffled as to why it isn’t working. Will try change to Call Service instead of Device Actions and see if that helps. Otherwise will just split them out again into separate automations.
I’ve tested using a shorter “on” time and it seems to be functioning. Will test gradually longer times and see at what point it stops working. Still really odd as the other one is working even at the 2 hour “on” time.
Have also updated the triggers and actions to use “State” and “Call Service” instead of “Device” types.
Not the last few times I noticed it didn’t trigger, nothing reset the for time, as I have a template sensor that uses that time to tell me how long the switch has been on for and it was showing over 2 hours. I assume it would reset for that template sensor as well if that was the case. However, I’ll check the history in my next test to see if is something weird going on there.
Could it be an automation mode issue? If both both switches hit the 2hr mark at the same time only one will be turned off because your automation is set to single mode. Try switching the mode to parallel.
Will try this out, but these are almost never on at the same time. I know for sure yesterday the Ensuite wasn’t switched on at all during the time the Bathroom one was on. But still no luck. I’ll change it to Parallel though as it’s a good call out in case they are used at the same time. Thanks!
Posting an update for anyone stumbling on this post.
Switching the Automation mode to Parallel is definitely something that helped for this one. But I think there was also an issue using a trigger that required a long period of time to pass.
One way I found that avoided potential issues was to create a template that tracked how long the switch was turned on. Then using the state of this template as the trigger. This means the template state isn’t impacted by any changes you may make to an automation, hence the trigger will still run appropriately. Whereas if you relied on the state a switch was active as a trigger, it would reset or not run properly if you’ve updated any automation during that time.
This is the template I’ve set up to track the time that the switch is on. I also use this as a way to show how long the towel rail has been on in my dashboard:
{% set ensuitetowel = 0 if states('switch.ensuite_heated_towel_rail_switch') == 'off' else now().timestamp() - states.switch.ensuite_heated_towel_rail_switch.last_changed.timestamp() %}
{{ ensuitetowel | timestamp_custom('%-H:%M', false) }}
{% set x = states('sensor.time') %}
I then use switch.ensuite_heated_towel_rail_switch as a trigger in the automation:
platform: state
entity_id: sensor.ensuite_towel_rail_running_time
to: '2:30'
id: ensuite
Maybe this isn’t best practice. But it means that even while the switch is on, I can go in and edit the automation to trigger at a different time and it will still run.