Exceptions on automation

Suppose we have an automation that turns on a bulb at 3pm and off at 5pm, every day.

Suppose that the bulb (a simple wifi bulb) goes offline at 2.59pm and goes online at 3.02 for whatever reason (connectivity etc…). The automation will not turn on the bulb because it was offline.

Is there a simple way that double check the condition of the bulb, and when it comes online again, it turns on/off the bulb anyway (even if it is out of trgger time), according the automation?

…I mean without writing other automations based on time trigger (e.g. every minute) that check the status of the bulb.

Start the automation with a condition ? Then use ‘choose’ to define what you want

Add a State Trigger that monitors the bulb’s state when it changes from unavailable (to any other state). If this state-change occurs between 3 and 5 PM (using a condition), the automation can turn on the bulb.

No. Please, look at this automation example:

- alias: TurnOnBulb
  trigger:
    - platform: time
      at: "15:00:00"
  action:
    - service: light.turn_on
      data:
        entity_id:
          - light.mybulb

- alias: TurnOffBulb
  trigger:
    - platform: time
      at: "17:00:00"
  action:
    - service: light.turn_off
      data:
        entity_id:
          - light.mybulb

What I do expect is that the bulb be ON between 15:00 and 17:00, and OFF in the remaining time.
If the bulb is always online, NO PROBLEM. It will be turned ON (or OFF) by automation.

But, if the bulb goes offline at 14.59 and become online again at 15:02, the bulb will be remain OFF.

I want the bulb be turned ON at 15.00. If it is offline at 15.00, I want it be turned ON as soon as it becomes online at 15.00 < t < 17.00, according the automation

I could re-write an automation like:

  trigger:
    - platform: time_pattern
      minutes: "/1"
  condition: 
    - condition: time
      after: "15:00:00"
      before: "17:00:00"
  action:
    - service: light.turn_on
      data:
        entity_id:
          - light.mybulb

But it is not elegant to use a trigger every minute. Are there alternative?

It will if you implement my suggestion.

- alias: TurnOnOffBulb
  trigger:
    - id: 'on'
      platform: time
      at: '15:00:00'
    - id: 'off'
      platform: time
      at: '17:00:00'
    - id: 'on'
      platform: state
      entity_id: light.mybulb
      from: 'unavailable'
  condition: "{{ today_at('15:00:00') <= now() < today_at('17:00:02') }}"
  action:
    - service: 'light.turn_{{ trigger.id }}'
      target:
        entity_id: light.mybulb

If the the bulb loses its Wi-Fi connection, Home Assistant reports its state as being unavailable. If the bulb’s state is unavailable at 15:00 the automation’s command to turn it on will fail to work. However, let’s say the bulb reconnects to Wi-Fi at 15:06. Its state will change from unavailable to (probably) off. That state-change will be detected by the State Trigger and the action will turn on the light bulb.

The condition is needed to ensure the bulb is controlled exclusively during the desired time range.

Clear! It is a good starting point

Thanks!

Maybe another simple and complete way can be:

- alias: TurnOnOffBulb
  trigger:
   -  platform: time
      at: '15:00:00'
   -  platform: time
      at: '17:00:00'
   -  platform: state
      entity_id: light.mybulb
      from: 'unavailable'
  condition: []
  action:
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ today_at('15:00:00') <= now() <= today_at('17:00:00') }}"
            sequence:
              - service: light.turn_on
                target:
                  entity_id: light.mybulb
        default:
              - service: light.turn_off
                target:
                  entity_id: light.mybulb

Using action based on conditions.

That’s yet another way to do it but not necessarily a simpler way.

  • In the first example, each trigger specifies which action (turn the light on or off) should occur (using the trigger’s id). The service call doesn’t perform any decision and simply performs the specified action.

  • In the second example, each trigger does not specify which action should occur. Therefore the automation’s action requires a choose to determine if it should turn the light on or off.

They both do the same thing but the second one requires slightly more code to achieve the desired result.