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
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.
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?
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.)