Looking for the best way to consolidate this automation.
If it is cloudy or rainy, then lights will go off 10 min after sunset
if it is not rainy or cloudy, then lights will go off when sun elevation is -3
I was thinking an idea would be to create a binary sensor for with all off the value logic, and and the result of it turning on will be the trigger in my automation.
Thinking about this some more is there a simpler approach to this? (Automation to check weather conditions at sun elevation -3, and another to check at sunset + 10 min, either will turn on a switch. When the switch is turned on, then the automation to turn the lights off will fire)
I took a quick look at both automations and feel that combining them is only worthwhile as an academic exercise. The end-result will not be more compact or easier to understand.
The consolidated automation will have multiple triggers so the service_template (or choose) will have to determine which trigger activated the automation. The condition's complexity will increase because it must also determine which trigger fired so that it can perform the correct test.
I really appreciate you looking at this. I have been driving myself nuts with this automation, in between posts, I looked more and more, and might have a solution, do you think this will kill 3 birds with 1 stone?
- alias: Exterior Lights Off
trigger:
- platform: numeric_state
entity_id: sun.sun
value_template: "{{ state_attr('sun.sun', 'elevation') }}"
above: -3
- platform: sun
event: sunrise
offset: "00:10:00"
condition:
- condition: template
value_template: >
{{ ((state_attr('sun.sun', 'elevation') | int > -3) and
states('weather.home') not in ('cloudy','rainy'))
or trigger.offset.total_seconds()/60 == 10 }}
action:
- service: script.exterior_lights_off
Unless you made a typo in the previous example, one automation’s condition did not want ‘cloudy’ or rainy whereas the other one did want it to be either ‘cloudy’ or ‘rainy’. This new version you produced appears to no longer make that distinction and settles on it not being ‘cloudy’ or ‘rainy’. Is that correct?
If the automation is triggered by numeric_state and it’s either cloudy or rainy, the result is false so it proceeds to evaluate the second half of the condition except trigger.offset doesn’t exist because the second trigger wasn’t responsible for triggering the automation.
You will have to check if trigger.offset is defined before you proceed to use it.
{{ ((state_attr('sun.sun', 'elevation') | int > -3) and
states('weather.home') not in ('cloudy','rainy'))
or (trigger.offset is defined and trigger.offset.total_seconds()/60 == 10) }}
I think I need to also need add another condition to compensate for the sun rising, that way it won’t fire when the sun is setting and the elevation is -3
Some have “>” and some “> -”, but none have “->” . The dash/hyphen removes whitespace, but it seems to rarely matter for the purpose of the templates we use in HA.
I don’t believe it works the way you think it does.
This:
state_attr('sun.sun', 'elevation') | int > -3
means ‘when elevation increases above -3’. When part of a trigger, that serves as a threshold. When the threshold is crossed, the trigger fires and is ‘latched’. It won’t keep re-triggering as the elevation continues to increase.
The trigger is ‘unlatched’, or reset, only when elevation re-crosses the threshold in the opposite direction (meaning when elevation falls below -3). Now it’s set and ready to be triggered again when the elevation rises above -3.
The takeaway here is that it triggers only at the moment when elevation rises above -3. It’s unidirectional and will only trigger for sunrises, not sunsets.
FWIW, you could even write the template like this:
{{ (trigger.to_state is defined and states('weather.home') not in ['cloudy','rainy'])
or (trigger.offset is defined and trigger.offset.total_seconds()/60 == 10) }}
@123, I’ve really been meaning out to say thank you! And apologize I haven’t! - So thank you!
I have another related question - when it comes to turning the lights on at night. I’m asking because weather around here has been beautiful at night and I’ve been looking at this way to long!
So 15 min before sunset, the trigger offset is defined (which I’m assuming has fired) and it is cloudy then go to the action statement, otherwise when the numeric state of the sun is defined, then go to the action statement
Would something like this work?
trigger:
- platform: numeric_state
entity_id: sun.sun
value_template: "{{ state_attr('sun.sun', 'elevation') }}"
below: -3
- platform: sun
event: sunset
offset: "-00:15:00"
condition:
- condition: template
value_template: >-
{{ (trigger.offset is defined and states('weather.home') in ['cloudy','rainy'])
or trigger.to_state is defined }}
action:
...
For the last line of the value template, should it actually be this instead? (trigger.to_state is defined and trigger.below -3)
I am not live with this just yet as I have been testing for a little bit.
I have a test notification in the action now, so when criteria is met, it will send me a notification, but I’ve been getting 2 notifications, one for elevation and one for sun.
Is the best way around this to add a condition for each automation
trigger:
- platform: numeric_state
entity_id: sun.sun
value_template: "{{ state_attr('sun.sun', 'elevation') }}"
below: -3
- platform: sun
event: sunset
offset: "-00:15:00"
condition:
- condition: template
value_template: >-
{{ (trigger.offset is defined and states('weather.home') in ['cloudy','rainy'])
or trigger.to_state is defined }}
- condition: state
entity_id: input_boolean.exterior_lights
state: 'off'
action:
- service: homeassistant.turn_on
entity_id: input_boolean.exterior_lights
You can put a condition in the action. Put it after turning on the light and before sending the notification. Make the condition check for whichever one of the two triggers you want (elevation?) and then the notification will be sent only if the condition evaluates to true.