Noob question Xiaomi PIR lux sensor triggering TPLink Smart Plug

Hi, I can’t seem to get my first Automation to work, where am I going wrong?

I am trying to say that when the PIR sensor is above 400 lux and the Sun is above horizon (to avoid any night time switch on with headlights for example) then turn on the Smart Plug in the Garage (I have solar power and Garage Power Plug charges all the battery devices (drills, bicycle, vacuum, etc) so I want to maximise use of the solar).

Turn off if it drops below 300lx (with sun up)

A catch all turn off if sun not up and the Plug is still on, such that it has been overridden manually (or the PIR fails). Also how do I stop the second automation is it has been turned on manually?

- alias: 'Solar_Garage_Power_on'
  initial_state: false
  trigger: 
    platform: numeric_state
    entity_id: sensor.illumination_158d00024e70db
    above: 400
  condition: state
    entity_id: sun.sun
    state: above_horizon
  action:
    - service: homeassistant.turn.on
      entity_id: switch.garage_solar_power_control
#      state: 'on'
    
- alias: 'Solar_Garage_Power_off'
  trigger: 
    platform: numeric_state
    entity_id: sensor.illumination_158d00024e70db
    below: 300
#  condition:
#    condition: and
#    conditions:
#    - condition: state
#      entity_id: sun.sun
#      state: 'above_horizon'
  action:
    - service: homeassistant.turn.off
      entity_id: switch.garage_solar_power_control
#     state: 'off'

- alias: 'Solar_Garage_Power_off_night'
  trigger: 
    platform: state
    entity_id: switch.garage_solar_power_control
    state: on
  condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    - service: homeassistant.turn.off
      entity_id: switch.garage_solar_power_control

I would try something like this. First I would create an input_boolean:

input_boolean:
  solar_auto_on:
    initial: off

Then these automations:

- alias: Solar Garage Power on
  trigger:
    platform: template
    value_template: >
      {{ states('sensor.illumination_158d00024e70db')|float > 400 and
         is_state('sun.sun', 'above_horizon') and
         is_state('switch.garage_solar_power_control', 'off') }}
  action:
    service: homeassistant.turn_on
    entity_id:
      - input_boolean.solar_auto_on
      - switch.garage_solar_power_control

When HA starts, if the expression evaluates to true (i.e., illumination is above 400 and the sun is up and the switch is off), then this will turn on the switch. After that the expression needs to evaluate to false and then to true for it to turn the switch on again. When it does turn on the switch it will also turn on an input_boolean to record that the switch was turned on automatically (which will be used below.)

- alias: Solar Garage Power off
  trigger:
    platform: numeric_state
    entity_id: sensor.illumination_158d00024e70db
    below: 300
  condition:
    - condition: state
      entity_id: sun.sun
      state: above_horizon
    - condition: state
      entity_id: input_boolean.solar_auto_on
      state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id:
      - input_boolean.solar_auto_on
      - switch.garage_solar_power_control

When the illumination drops below 300, and if the sun is still up, and if the switch was turned on automatically (by the first automation), then the switch will be turned off (and the input_boolean will be “reset”.)

- alias: Solar Garage Power off night
  trigger:
    platform: sun
    event: sunset
  action:
    service: homeassistant.turn_off
    entity_id:
      - input_boolean.solar_auto_on
      - switch.garage_solar_power_control

When the sun sets it will turn off the switch (regardless of how it was turned on), and also “reset” the input_boolean in case it was on.

Thanks, that works really well, now I have some more learning to do

1 Like