Automation based on the state of a schedule rather than a s a trigger

I want to use the the state of a schedule to control a device rather than use it the schedule as a trigger to turn on/off the device. This seems much more secure to me as for some reason the device randomly misses the trigger (on at 1600, off at 2330). I think this should be easy, but have yet to be able to extract the state of the schedule apart from within its history…

How are you proposing to do that?

What do you mean by this?

As fo this:

Do you mean the automation misses the trigger or the device misses the action (check the automation traces)?

By “state of the schedule”, I mean that at any time, I want to be able to see of the schedule is in the on or off phase. This means that if a device is not available (for any reason) at the trigger/transition point (eg turn on at 1600), as soon as it becomes available, the automation will turn it on or off as applicable. I would have thought this would be relatively simple…?

What have you read in the documentation that led you to believe it was simple?

There’s a schedule helper that turns its state on and off on the requested time. Create one in the helper section and fill the times you want the light to be on. You can use the state of that schedule helper to decide when to turn a light on or off. If you add a trigger for when the light becomes available, then you can use the state of the schedule to decide what state the light should have as well.

description: "Faulty light scheduler"
mode: single
trigger:
  - platform: state
    entity_id:
      - schedule.myschedule
    to:
      - "on"
      - "off"
  - platform: state
    entity_id:
      - light.mylight
    from:
      - unavailable
      - unknown
condition: []
action:
  - service: light.turn_{{ states('schedule.myschedule') }}
    target:
      entity_id: light.mylight
    data: {}

However, best to always avoid lights going unavailable, especially for zigbee and z-wave. So fixing that would be best.

1 Like

I think what you’re getting at is that you want some entity to be in a specific state during some set of conditions. If the entity is unavailable or HA is off, the entity should be placed into that desired state as soon as the entity becomes available or HA is back online. And if someone or some other automation changes the state of the entity, HA will fight back and revert it to the desired state.

If you’re using the schedule helper, this is actually pretty easy. Let’s say you created a schedule, and you want a light to be on whenever that schedule is active and a certain door is closed, no matter what (even if someone tries to turn that light off). And if the schedule is not active, or the door is open, you want the light off no matter what.

To do this, you just need to set up an automation with matching triggers and conditions:

Triggers:

  • state of light (any state)
  • state of schedule (any state)
  • state of door (any state)

Conditions:
None

Actions:

  • Choose:
    • Option 1 Conditions
      • state of schedule is on
      • state of door is closed
    • Option 1 Action:
      • call service: turn light on
    • Default Action:
      • call service: turn light off

Where this actually gets slightly more challenging is if you don’t use the schedule helper and instead use time triggers in your automation. In that case, when you miss a time trigger (if HA is down, for example) then the automation won’t fire when HA comes back up. What you need to do in that case is either use a schedule helper as already discussed, or use a template trigger which evaluates if now() is between two datetimes.

I would love to see HA support a “time range” automation trigger but it would probably confuse more people than it would help.

1 Like