Conditional automation (AND and an OR) not working

I’m going mad trying to debug this and would really appreciate some help, please.

What I’m trying to achieve is detect when either of the two Tradfri bulbs are turned on (an OR condition) and then if the sun is above a certain height (the AND condition) it should turn off the two lights and wait for the next time one of the bulbs gets turned on (either through the mains switch being turned on or the motion sensor turning it on).

It checks out OK formatting but does not work. When I enable it, it automatically disables itself (or is disabled by something unknown) within seconds.

I’ve tried inverting the sun height logic by making it below a certain elevation but it then has the same effect.

What am I doing wrong, please?

And as a subsequent question, is there a way to run traces and debugs on HASS?

  - alias: 'Daylight Stair Motion Sensor Power Saver'
    initial_state: true
    trigger:
    condition:
      condition: and
      conditions: 
# first condition to AND
        - condition: numeric_state
          entity_id: sun.sun
          value_template: "{{ state.attributes.elevation }}"
          above: 0.0
# end first AND condition
        - condition: or
          conditions:
          - condition: state
            entity_id: 'light.stairs_tradfri'
            state: 'on'
          - condition: state
            entity_id: 'light.tradfri_bulb'
            state: 'on'
    action:
    - service: homeassistant.turn_off
      data:
         entity_id:
            - light.stairs_tradfri
            - light.tradfri_bulb

Thanks very much.

Richard

(note: edited to fix a typo)

Your trigger is empty. I just tried to create an automation with an empty trigger, and it turns itself off after a few seconds.

To turn off the lights if the sun is “up” I think this should do it (even though they will turn on and then off again which might look strange)

  - alias: 'Daylight Stair Motion Sensor Power Saver'
    initial_state: true
    trigger:
      - platform: state
        to: 'on'
        entity_id:
          - light.stairs_tradfri
          - light.tradfri_bulb
    condition:
      - condition: numeric_state
        entity_id: sun.sun
        value_template: "{{ state.attributes.elevation }}"
        above: 0.0
    action:
      - service: homeassistant.turn_off
        data:
           entity_id:
             - light.stairs_tradfri
             - light.tradfri_bulb
1 Like

Oh, that makes sense and I missed it! Thank you!

The Tradfri system pairs the motion sensor to the bulb so there is no opportunity to intercept it with HA. Unfortunately the built-in day/night sensor is not great and continues to light the bulbs even when the ambient light level is bright.

In my experimentation (before I added the sun part of the code), the on-off cycle was very quick and won’t really be noticeable in the light.

Thanks again for your help.

The trigger part of an automation controls when the action(s) run, and the condition part controls if the action(s) run (when a trigger event occurs.) So the question is, when do you want to turn off the lights? When a light goes on (if the sun is high enough), when the sun goes above a certain elevation (if a light is on), or both?

This is to cover the time from when it get light until the Tradfri light sensor stops the motion sensor lighting the bulbs - and the reverse in the evenings.

Ok, then @e02jr’s suggestion seems like it’s what you’re looking for (with maybe adjusting the above value for the elevation.) Then you don’t care if the light is already on when the sun gets to that elevation?

Yes, it seems to be working, thanks.

The lights will predominantly be controlled by the motion sensors - once the family get used to them they will stop using the switches. The ‘on’ period from the motion sensor is about 2 minutes so the lights should not be on for extended periods at any time. If I find that they are then I’ll have to extend the automation to cover that scenario as well but thanks for considering that and making me aware of it!

No problem. FWIW, if you do ever want to do it, probably the easiest way is with a template trigger (and in this case you won’t need the additional conditions, because they have been “folded into” the trigger):

trigger:
  platform: template
  value_template: >
    {{ (is_state('light.stairs_tradfri', 'on') or
        is_state('light.tradfri_bulb', 'on')) and
        state_attr('sun.sun', 'elevation') > 0 }}

The trigger will be evaluated whenever any of the referenced entities changes. The trigger will “fire” whenever the result of the expression changes from False to True. (Also, the first time it is evaluated after HA starts, if it is True, it will “fire” then as well.)

You make a compelling case with that code, pnbruckner!

Is this what the full automation would look like?

- alias: 'Daylight Stair Motion Sensor Power Saver'
    initial_state: true
    trigger:
      platform: template
      value_template: >
        {{ (is_state('light.stairs_tradfri', 'on') or
        is_state('light.tradfri_bulb', 'on')) and
        state_attr('sun.sun', 'elevation') > 0 }}
    action:
      - service: homeassistant.turn_off
        data:
           entity_id:
             - light.stairs_tradfri
             - light.tradfri_bulb

If so, I’ll use it - it is clean and neat and perhaps saves me further work later on!

Thanks

And I learned an even better way to write automations :grinning:

Me too. I’m new to YAML so am finding it a bit of a steep learning curve so that is very welcome. Thank you both so much!

i would also not work with sun down or up… but sun elevation … as u can play with this that the sun is below the horizon … and change the offsets.

I’m new to this but I think I am, Dom. By changing the 'elevation') > 0 section I can amend the elevation unless I’m misunderstanding this…

Close, but the indentation was a bit off. And it can be simplified a touch. Try:

- alias: 'Daylight Stair Motion Sensor Power Saver'
  initial_state: true
  trigger:
    platform: template
    value_template: >
      {{ (is_state('light.stairs_tradfri', 'on') or
          is_state('light.tradfri_bulb', 'on')) and
         state_attr('sun.sun', 'elevation') > 0 }}
  action:
    service: homeassistant.turn_off
    entity_id:
      - light.stairs_tradfri
      - light.tradfri_bulb

Thanks again, pnbruckner - that seems to be working.

1 Like