I’ve been working on an automation for my front porch lights that works as follows:
When the sun is 1 degree below the horizon, activate a random Hue scene from a script. When motion is activated, turn the lights on full bright. After 1 minute of no motion, run the random scene script again. I am having some issues getting it to work correctly so I am looking for feedback on my yaml where I might be able to make improvements. I haven’t been able to set it up to just turn off when the sun it 1 degree above the horizon. I prefer to work on degrees of horizon rather than time to account for the changing times for sunset and rise since elevation is absolute. I’ve considered stripping it into 2 separate automatons but I’d like to make it work in one, if possible. Anyhow, here is what I have thus far:
I’m not sure why this is not working as I cannot see anything obviously wrong other than the fact that the condition will prevent the automation from running in the situation you describe where the sun is 1 degree above the horizon.
I have tended to use the choose command rather than if-then-else in these applications and the following is an example that has been working well for a few years.
Thanks for the reply. I was just looking at the choose command this afternoon. I’ve never used it and actually started.looking at documentation to learn about it today. I’ll look over your example when I’m not on mobile and will see how I might incorporate it. Thanks again!
For your consideration (the script’s functionality is incorporated into the automation but can be easily moved back to a script if that’s your preference).
alias: Front Porch Full_Random Scenes
description: ''
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
below: -1
- platform: state
entity_id: binary_sensor.hue_motion_sensor_1_motion
from: 'off'
to: 'on'
- platform: state
entity_id: binary_sensor.hue_motion_sensor_1_motion
from: 'on'
to: 'off'
for:
minutes: 1
condition:
- condition: numeric_state
entity_id: sun.sun
attribute: elevation
below: -1
action:
- service: scene.turn_on
target:
entity_id: >
{% if trigger.platform == 'numeric_state' or trigger.to_state.state == 'off' %}
{% set scenes = ['savanna_sunset', 'meriete', 'moonlight', 'starlight',
'forest_adventure', 'magneto', 'arctic_aurora', 'memento', 'motown'] %}
scene.front_porch_{{ scenes | random }}
{% else %}
scene.front_porch_bright
{% endif %}
mode: single
A random scene is activated when the sun sets or when the motion sensor turns off.
The front porch light is turned on when the motion sensor turns on.
EDIT
Simplified the action’s logic. Only one scene.turn_on service call is needed.
@123 That is an excellent simplification. I appreciate the feedback. I am going to try and implement this and see how it works. Again, many thanks!
edit: looking over this again I am again reminded how much more robust this platform is when templates are used. Another reminder that I need to really learn how to use those in my automations. Thanks again!
@123 Ok cool, thanks for the reply! I am setting it up and will see how it runs tonight, I’ll let you know.
Edit: Wouldn’t you know… I went to do some testing and discovered my Hue sensor failed last night and I think it is finally dead because I can’t get it to power on at all. Time for a new sensor and then I’ll be able to report back how it all functions.
@123 Just a quick reply, automation worked flawlessly last night. I fiddled with my motion sensor last night and got it to sort of work and got great results. Thanks again for the help!