Automation using Trigger IDs works for one trigger and not the other

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.

What am I missing?!

    alias: Heated Towel Rail - Off
    description: ''
    trigger:
      - platform: device
        type: turned_on
        entity_id: switch.bathroom_heated_towel_rail_switch
        domain: switch
        for:
          hours: 2
          minutes: 0
          seconds: 0
        id: bathroom
      - platform: device
        type: turned_on
        entity_id: switch.ensuite_heated_towel_rail_switch
        domain: switch
        for:
          hours: 2
          minutes: 0
          seconds: 0
        id: ensuite
    condition: []
    action:
      - choose:
          - conditions:
              - condition: trigger
                id: bathroom
            sequence:
              - type: turn_off
                entity_id: switch.bathroom_heated_towel_rail_switch
                domain: switch
          - conditions:
              - condition: trigger
                id: ensuite
            sequence:
              - type: turn_off
                entity_id: switch.ensuite_heated_towel_rail_switch
                domain: switch
        default: []
    mode: single

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:

trigger:
  - platform: state
    entity_id:
      - switch.bathroom_heated_towel_rail_switch
      - switch.ensuite_heated_towel_rail_switch
    for:
      hours: 2

EDIT: Oh right, you have this:

          - conditions:
              - condition: trigger
                id: ensuite

Well you can either split back into two triggers with separate IDs or switch to something like this, whichever you find easier:

          - conditions: "{{ trigger.entity_id == 'switch.bathroom_heated_towel_rail_switch' }}"

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.

1 Like

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.

Does the state change in developer tools / states?

Is the entity id correct?

Service calls and state triggers:

    alias: Heated Towel Rail - Off
    description: ''
    trigger:
      - platform: state
        entity_id: switch.bathroom_heated_towel_rail_switch
        from: 'off'
        to: 'on'
        for:
          hours: 2
          minutes: 0
          seconds: 0
        id: bathroom
      - platform: state
        entity_id: switch.ensuite_heated_towel_rail_switch
        from: 'off'
        to: 'on'
        for:
          hours: 2
          minutes: 0
          seconds: 0
        id: ensuite
    condition: []
    action:
      - choose:
          - conditions:
              - condition: trigger
                id: bathroom
            sequence:
              - service: switch.turn_on
                target:
                  entity_id: switch.bathroom_heated_towel_rail_switch
          - conditions:
              - condition: trigger
                id: ensuite
            sequence:
              - service: switch.turn_on
                target:
                  entity_id: switch.ensuite_heated_towel_rail_switch
        default: []
    mode: single

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 notes for 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.

1 Like

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.

Is the device becoming momentarily unavailable during the 2 hours (check the entity history)?

That will reset the for time.

1 Like
trigger:
  - platform: state
    entity_id: switch.bathroom_heated_towel_rail_switch
    from: 'off'
    to: 'on'
    for:
      hours: 2
      minutes: 0
      seconds: 0

I am using quite a few state/time triggers here but I format them differently:

    trigger:
      - platform: state
        entity_id: switch.bathroom_heated_towel_rail_switch
        from: 'off'
        to: 'on'
      	for: '02:00:00'

Maybe worth a try?

I believe they are using the automation editor. You have to supply hours, minutes and seconds using that.

And they should be equivalent.

Oh OK. Since I am using yaml for all I didn’t know that. One never stops learning :+1:t3:

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.

3 Likes

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.