Read state from TP-Link HS110 after restarting Hassbian to perform missed automation

Hi Guys,

I’ve got a few TP-Link HS110’s switching a couple of lamps around the house, and for they work very well, I have nutted out time offsets using the sunrise/sunset for the kids nightlights, etc.

The issue I have is that I’m finding that for some reason, Hassbian needs a reboot most days and if I’ve missed this before I go to work and the kids nightlights are on while the Pi is requiring a reboot that Home Assistant isn’t reading the state if it’s missed the trigger time.

My YAML code for one of the switches is as below

- alias: Night Light On
  initial_state: True
  hide_entity: True
  trigger:
    platform: sun
    event: sunset
    offset: '-00:45:00'
  action:
    service: homeassistant.turn_on
    entity_id: switch.night_light

- alias: Night Light Off
  trigger:
    platform: sun
    event: sunrise
    offset: '-00:45:00'
  action:
      service: homeassistant.turn_off
      entity_id: switch.night_light

Hoping someone can point me in the right direction - I’ve done some searching, and unable to find how to sort it out…

Cheers

No sure I understand correctly but HA gets the on/off state from the switch. So if you switch it off during a reboot HA will know it’s switched off.

thanks @sjee - the issue I have is that if the Pi freezes (I think it’s a memory leak, but can’t prove it yet), and misses the automation trigger, I have a set of lights that either don’t turn on, or don’t turn off.

e.g. today, the Pi locked up sometime around 5am, so it didn’t turn off the kids’ night lights, as sunrise was around 7:15, so the automation would normally have turned off the lights at approx 6:30 …

Maybe my question is “how do I ensure a missed automation runs after Hassbian is restarted”

I see, in that case I would use a time trigger with for example a 5 minutes interval with a condition on sunrise/sunset and switch on/off.

would that look something like this?

- alias: Night Light On
  initial_state: True
  hide_entity: True
  trigger:
    platform: time
    minutes: 5
    platform: sun
    event: sunset
    offset: '-00:45:00'
  action:
    service: homeassistant.turn_on
    entity_id: switch.night_light

You can add a trigger for HA starting to your automations. I do this for a lot of my automations. Something like this:

- alias: Night Light On
  initial_state: True
  hide_entity: True
  trigger:
    - platform: sun
      event: sunset
      offset: '-00:45:00'
    - platform: homeassistant
      event: start
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:45:00'
      - condition: sun
        before: sunrise
        before_offset: '-00:45:00'
  action:
    service: switch.turn_on
    entity_id: switch.night_light

- alias: Night Light Off
  trigger:
    - platform: sun
      event: sunrise
      offset: '-00:45:00'
    - platform: homeassistant
      event: start
  condition:
    - condition: sun
      after: sunrise
      after_offset: '-00:45:00'
    - condition: sun
      before: sunset
      before_offset: '-00:45:00'
  action:
      service: switch.turn_off
      entity_id: switch.night_light

These automations will trigger either at the desired time relative to sunrise or sunset, or they will trigger when HA starts. For the latter the conditions will make sure the correct actions run depending on the time of day relative to sunrise and sunset.

BTW, if you’re turning a switch on or off then it’s better to use the switch.turn_off and switch.turn_on services. You can use the homeassistant.turn_off and homeassistant.turn_on services, but they will just turn around and call the switch services, so you might was well just use the switch services directly (since you’re only controlling a switch. If you were turning switches, lights, etc on or off at the same time, then the homeassistant services are the right ones to use.)

2 Likes

thanks @pnbruckner, I’ll give that a whirl and see how I go :slight_smile:

that works perfectly @pnbruckner!

can someone help me regarding the condition function - what have I got wrong here for getting it to turn on/off a light based on time if HASS is restarted?

- alias: Living Room Lamp Off
  trigger:
    - platform: time
      at: '22:15:00'
    - platform: homeassistant
      event: start
  condition:
    - condition: time
      after: '22:15:00'
  action:
    - service: switch.turn_off
      entity_id: switch.upstairs_living_room

I’ve also tried this to no avail:

- alias: Living Room Lamp Off
  trigger:
    - platform: time
      at: '22:15:00'
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: switch.upstairs_living_room
      state: 'on'
    - condition: time
      after: '22:15:00'
  action:
    - service: switch.turn_off
      entity_id: switch.upstairs_living_room

You shouldn’t need to add the condition for the switch being off, although that shouldn’t hurt. In general it looks ok. The only thing I can think of is if the switch platform isn’t ready when the homeassistant_start event is fired. That’s possible. In which case you may need to add a delay, which, BTW, is what I do:

- alias: Delayed Startup
  trigger:
    platform: homeassistant
    event: start
  action:
    - delay: 15
    - event: DELAYED_HOMEASSISTANT_START
- alias: Living Room Lamp Off
  trigger:
    - platform: time
      at: '22:15:00'
    - platform: event
      event_type: DELAYED_HOMEASSISTANT_START
  condition:
    - condition: time
      after: '22:15:00'
  action:
    - service: switch.turn_off
      entity_id: switch.upstairs_living_room

Also note that the condition you wrote means between 22:15:00 and midnight. The action will not run after midnight. If you want it to run up to some time in the morning, then you’ll need to add a before: parameter to the condition.

Would changing the after condition to 22:14:59 help?

That shouldn’t be necessary either. I have automations just like this (i.e., trigger at a particular time and check that it’s after that exact same time in the condition.) Also the OP, although not stated explicitly, implies the problem is not with the time trigger, but with the homeassistant start event.

1 Like

Thanks, @pnbruckner - it was one of these post-first-then-read-cases :frowning_face:

Good to know, though, that the at-trigger and the after-condition can actually contain the same time.

thanks again @pnbruckner … I completely missed the 22:15 - 23:59:59 issue, with the multiple conditions.

I’ll give this a shot tonight - I think that should catch the issue of after midnight by ensuring it will turn off before sunrise and/or sunset? To me it doesn’t look quite elegant enough, but if it works…

- alias: Living Room Lamp Off
  trigger:
    - platform: time
      at: '22:15:00'
    - platform: homeassistant
      event: start
  condition:
    - condition: time
      after: '22:15:00'
    - condition: sun
      before: sunrise
    - condition: sun
      before: sunset
  action:
    - service: switch.turn_off
      entity_id: switch.upstairs_living_room

Conditions are and’d, so what you wrote means the light will only turned off it it’s between 22:15 and midnight, AND before sunrise, AND before sunset. Hmm, I don’t think that will ever be true.

What time period do you want the light to turn off if HA restarts? Let’s say you want between 22:15 and sunrise the next morning. Then you’d want this:

- alias: Living Room Lamp Off
  trigger:
    - platform: time
      at: '22:15:00'
    - platform: homeassistant
      event: start
  condition:
    condition: or
    conditions:
    - condition: time
      after: '22:15:00'
    - condition: sun
      before: sunrise
  action:
    - service: switch.turn_off
      entity_id: switch.upstairs_living_room

So now the condition is between 22:15 and midnight, OR between midnight and sunrise.

the turn on condition is 15 minutes after sunset, and in the event HA has to restart during the on or the off cycle, or for some reason it hangs (like yesterday), I’m just trying to ensure the state is as expected when it restarts…

I think I have it nailed now :slight_smile:

- alias: Living Room Lamp On
  initial_state: True
  hide_entity: True
  trigger:
    - platform: sun
      event: sunset
      offset: '00:15:00'
    - platform: homeassistant
      event: start
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '00:15:00'
  action:
    service: switch.turn_on
    entity_id: switch.upstairs_living_room

then turn off with:

- alias: Living Room Lamp Off
  trigger:
    - platform: time
      at: '22:15:00'
    - platform: homeassistant
      event: start
  condition:
    condition: or
    conditions:
    - condition: time
      after: '22:15:00'
    - condition: sun
      before: sunrise
    - condition: sun
      before: sunset
  action:
    - service: switch.turn_off
      entity_id: switch.upstairs_living_room