Ask for best practices (on / off by schedule)

Hi!
For example, we have a simple automation.
Sunset - light on
Sunrise - light off

Today sunset at 7:15PM, but at 7:00PM - power outage.
For example, power restored at 9pm.

My question is: how to solve the issue for restoring missed event at 7:15pm?
The same issue with sunrise event as well.

NB: checking for status “is on” + time period every N min is ugly. IMHO.

Thank you!

WBR,
O.

home assistant fires this event when it’s restarted. you can trigger off of that instead of checking every minute. you’d still have to check the expected state tho.

trigger:
  - platform: homeassistant
    event: start
2 Likes
1 Like

HASS on the UPS. =) It doesn’t care =) It can work above 10h

1 Like

it’s OK. Thank you Taras.
but. If I have for example 2 lights.* 3 switch.* I should create groups entity that represents multiple entities of the same type and handle lights and switch together via and
is_state('light.exterior', 'on') and is_state('switch.exterior', 'on')

If you want, you can do it like this:

alias: 'Scheduled Exterior'
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 1.8
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: -3.1
  - platform: homeassistant
    event: start
action:
  - variables:
      e: "{{ state_attr('sun.sun', 'elevation') | float }}"
      entities:
        - light.one
        - light.two
        - switch.one
        - switch.two
        - switch.three
      entities_on: >
        {{ expand(entities) | selectattr('state', 'eq', 'on')
          | map(attribute='entity_id') | list }}
      entities_off: >
        {{ expand(entities) | selectattr('state', 'eq', 'off')
          | map(attribute='entity_id') | list }}
  - choose:
      - conditions:
          - "{{ e <= 1.8 }}"
          - "{{ entities_off | count > 0 }}"
        sequence:
          - service: homeassistant.turn_on
            target:
              entity_id: "{{ entities_off  }}"
      - conditions:
          - "{{ e >= -3.1 }}"
          - "{{  entities_on | count > 0  }}"
        sequence:
          - service: homeassistant.turn_off
            target:
              entity_id: "{{ entities_on }}"
    default: []

EDIT

Correction. Added missing | to templates for entities_on and entities_off.

1 Like

OMG! yep, Sir. I forgot what we can define entities via list.
Thank you so much!

1 Like

you miss |

{{ expand(entities) | selectattr('state', 'eq', 'on') |
          map(attribute='entity_id') | list }}
1 Like