Help With A Template / Automation

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.

- alias: Exterior Lights - Elevation - Off
  initial_state: true
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"
      above: -3
  condition:
    - condition: template
      value_template: {{ (not is_state('weather.home', 'cloudy')
                       or not is_state('weather.home', 'rainy'))
                       and state_attr('sun.sun', 'rising') == true}}
  action:
	- service: script.exterior_lights_off
	
- alias: Exterior Lights - Sunrise - Off
  initial_state: true
  trigger:
    - platform: sun
      event: sunrise
      offset: "00:10:00"
  condition:
    - condition: template
      value_template: {{ (is_state('weather.home', 'cloudy')
                       or is_state('weather.home', 'rainy'))
                       and state_attr('sun.sun', 'rising') == true}}
	- service: script.exterior_lights_off	

@petro Can I kindly ask you to have a look and offer a suggestion?

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)

- alias: Exterior Lights Trigger - Elevation - Off
  initial_state: true
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"
      above: -3
  condition:
    - condition: template
      value_template: {{ (not is_state('weather.home', 'cloudy')
                       or not is_state('weather.home', 'rainy')) }}
    - condition: state
      entity_id: switch.exterior_lights_off
	  state: 'off'		
   action:	
	service: homeassistant.turn_on
	entity_id: switch.exterior_lights_off
	
- alias: Exterior Lights Trigger - Sunrise - Off
  initial_state: true
  trigger:
    - platform: sun
      event: sunrise
      offset: "00:10:00"
  condition:
    - condition: template
      value_template: {{ (is_state('weather.home', 'cloudy')
                       or is_state('weather.home', 'rainy')) }}
    - condition: state
      entity_id: switch.exterior_lights_off
	  state: 'off'					   
   action:	
	service: homeassistant.turn_on
	entity_id: switch.exterior_lights_off
	
- alias: Exterior Lights Off
  initial_state: true
  trigger:
    - platform: state
      entity_id: switch.exterior_lights_off
      state: 'on'
  action:
	- service: script.exterior_lights_off
	- service: homeassistant.turn_off
	  entity_id: switch.exterior_lights_off

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.

Thank you. So essentially what is in my second post is the better option?

I took your second version and simply streamlined the templates:

- alias: Exterior Lights Trigger - Elevation - Off
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"
      above: -3
  condition:
    - condition: template
      value_template: >
        {{ states('weather.home') not in ['cloudy', 'rainy'] and
           is_state('switch.exterior_lights_off', 'off') }}
   action:	
     - service: homeassistant.turn_on
       entity_id: switch.exterior_lights_off
	
- alias: Exterior Lights Trigger - Sunrise - Off
  trigger:
    - platform: sun
      event: sunrise
      offset: "00:10:00"
  condition:
    - condition: template
      value_template: >
        {{ states('weather.home') in ['cloudy', 'rainy'] and
           is_state('switch.exterior_lights_off', 'off') }}
  action:	
    - service: homeassistant.turn_on
      entity_id: switch.exterior_lights_off
	
- alias: Exterior Lights Off
  trigger:
    - platform: state
      entity_id: switch.exterior_lights_off
      state: 'on'
  action:
    - service: script.exterior_lights_off
    - service: homeassistant.turn_off
      entity_id: switch.exterior_lights_off

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?

When sun elevation is at -3 and not cloudy or rainy, turn off, otherwise it would use the sunrise offset which would assume it is cloudy or rainy.

Not quite.

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) }}

Thank you!

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


- condition: template
  value_template: >
  {{ state_attr('sun.sun', 'rising') == true }}

Please refresh my memory, with a value_template, I see some that have value_template > and value_template ->, which is correct?

Some have “>” and some “> -”, but none have “->:slight_smile:. The dash/hyphen removes whitespace, but it seems to rarely matter for the purpose of the templates we use in HA.

Haha, thank you @rccoleman rccoleman!

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:
   ...

I think it’ll work.

Hi! Thanks!

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.

I think this 2-week old thread has plenty of ideas and you should pick one and run with it.