Automation With Conditions

I am trying to setup a light to turn on via motion sensor. I have it working between the hours of 4:30pm to 10:30pm. But want to see if I can change to either only if sun has set so it will also work in the morning before work while getting ready. I can’t seem to get it working with what I put in under sun. So in the meantime I have a 2nd automation setup for 4:00am to 7:00am

If you want the sun to have set, use the state of below_horizon for sun.sun:

condition:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'

Does your motion sensor also have an illuminance / light sensor perchance?

Personally I use illuminance where I can instead of sun - that was if it’s cloudy before sunset - I really want lights to trigger earlier vs. ‘just’ when the sun sets.

Understand if not, but figured I’d throw it out there.

If you don’t have illuminance, this should work as conditions for only BEFORE sunrise or AFTER sunset:

condition: or
conditions:
  - condition: sun
    before: sunrise
  - condition: sun
    after: sunset

I forgot to add what I am using now for time. Let me get what I was trying with the Sun.

- id: '1579575223976'
  alias: Office Light On If Motion
  description: Turn on office light if motion between 4:30pm-10:30pm
  trigger:
  - entity_id: binary_sensor.wyzemotion
    platform: state
    to: 'on'
  condition:
  - after: '16:30'
    before: '22:30'
    condition: time
  action:
  - entity_id: light.office_light
    service: light.turn_on

Guess I’m not 100% clear on exactly WHEN you want the light to be turned ON by motion… Is it ONLY based on the sun, or ONLY based on the time, or some combination of both?

The UI editor in /config/automation/new is pretty good about allowing you to build fairly complex scenarios - and I’ve personally found that sometimes it’s better to write them out on paper before I dive in and start trying to code the complex ones into HA.

So if you want:
Turn on light when motion

Conditions:
Before sunrise
OR
Between 4am and 7am

AND

After sunset
OR
Between 4:30pm and 10:30pm

That can be coded, but just need to know specifically where the and/or’s you require are…

1 Like

You can also use templates within the automation for simplifying it a lot, reducing their number at a large scale after some months. :slight_smile:

- id: 'turn_on_off_that_switch'   
  alias: 'Turn ON/OFF that switch'
  trigger:
    - platform: state
      entity_id: sun.sun
  action:
    - entity_id: switch.whatever_its_name_is
      service_template: >
         {% if is_state('sun.sun', 'below_horizon') %}
           switch.turn_on
         {% elif is_state('sun.sun', 'above_horizon') %}
           switch.turn_off
         {% else %}
# for other future new sun.sun states or the 'unknown' one
           switch.turn_off
         {% endif %}

Or use conditions regarding the sun elevation, if your house is under the horizon line (in a valley or behind a mountain):

      - condition: numeric_state
        entity_id: sun.sun
        value_template: '{{ states.sun.sun.attributes.elevation }}'
        below: 9

Or even the first one with dependency for the sun elevation itself:

- id: 'turn_on_off_that_switch'   
  alias: 'Turn ON/OFF that switch'
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ states.sun.sun.attributes.elevation }}'
  action:
    - entity_id: switch.whatever_its_name_is
      service_template: >
         {% if 9 < (states.sun.sun.attributes.elevation)|int < 50 and is_state('switch.whatever_its_name_is','off') %}
           switch.turn_on
         {% elif not(9 < (states.sun.sun.attributes.elevation)|int < 50) and is_state('switch.whatever_its_name_is','on') %}
           switch.turn_off
         {% else %}
           switch.turn_off
         {% endif %}

The advantage of using the sun state or elevation is that rules the entire year around, even with DST changes.

@Markus99 this was very helpful, I think I got it set now and how you explained it make total sense now.

Thanks

1 Like

@ReDaLeRt Thanks for the explanation on templates, I will need to look into this once I get more items. Still looking at adding packages and splitting my files into easier to manage items instead of a big file.

Markus99, how do use light sensors? I do have several Aqara motion sensors with build in light sensor, but I found these to be very insensitive in low light; max reading I observed was ~550lux, but in normal conditions (when it is darkish) it reads in range of 1~5 lux only :open_mouth: ), which leaves very limited range for finding the right trigger point. Perhaps I should use more sensitive device… any advise?