Single Automation for Light at Night

Hi All,

I configured a light to come on after the sun sets, and turn off at sunrise in the following manner:

Having 3 automations seems wrong for such a simple task. (The third one ensures that the light is on if the server restarts). Can you suggest a way to combine those three into one?

The logic is painfully simple: if the sun is below the horizon, the light should be on.

Would I be right in assuming you would want to do this via the gui editors ?

I don’t mind doing it by hand, if that’s the only way. I was able to get this far via GUI, but I think the last bit is wrong:

- id: '1593801439995'
  alias: Fan ON when Excess Energy
  description: ''
  trigger:
  - entity_id: binary_sensor.excess_energy
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.excess_energy
    from: 'on'
    platform: state
    to: 'off'
  - event: start
    platform: homeassistant
  condition: []
  action:
  - condition: state
    entity_id: switch.aft_cabin_fan
    state: binary_sensor.excess_energy

in this particular case I want the state of switch.aft_cabin_fan to mirror the state of entity_id: binary_sensor.excess_energy

You talked in your first post about lights and sun positions.
You second post talks about fans and energy.
What are you after doing ?
What entity_id’s are involved ?

Logic is exactly the same: binary_sensor (sun in the first case, “excess_energy” in the second). I want to set the switch (light in the first case, fan in the second case) to mirror the state of that binary sensor. I can’t manipulate the sun, so I am experimenting with excess_energy :slight_smile: sorry for the confusion.

If you use a trigger such as this one:

 - platform: numeric_state
   entity_id: sun.sun
   value_template: "{{ state_attr('sun.sun', 'elevation') }}"
   below: 0.0

it will trigger when the sun’s elevation dips below the horizon. It will also trigger when Home Assistant is restarted when the sun is already below the horizon. That might help you consolidate your first and third automations (that ensure the anchor light is turned on after sunset).

1 Like

I think I finally got it:

  action:
  - entity_id: switch.aft_cabin_fan
    service_template: '{% if is_state(''binary_sensor.excess_energy'', ''on'') %}
      switch.turn_on {% else %} switch.turn_off {% endif %}'

Those single quotes are important!

The purpose of marking a post with the Solution tag is to show others how to solve the originally stated problem.

In this case, the Solution post doesn’t do that. It contains no triggers for sunrise or sunset and references entities not mentioned in the original problem.

For the benefit of the community, please consider revising your Solution post to show others how you reduced the original three automations.

2 Likes

Dear Taras, I posted the “action” bit because that is the only part that I was having problems with, and the only one that is counterintuitive, the rest of the yaml is the same as in my post above, and can be achieved with the GUI. So I didn’t want to confuse whomever is going to be reading this, and making an impression that I did the whole thing by hand. Here it is in its entirety:

- id: '1593801439995'
  alias: Fan ON when Excess Energy
  description: ''
  trigger:
  - entity_id: binary_sensor.excess_energy
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.excess_energy
    from: 'on'
    platform: state
    to: 'off'
  - event: start
    platform: homeassistant
  condition: []
  action:
  - entity_id: switch.aft_cabin_fan
    service_template: '{% if is_state(''binary_sensor.excess_energy'', ''on'') %}
      switch.turn_on {% else %} switch.turn_off {% endif %}'

and btw. your suggestion with numeric_state didn’t work. It didn’t trigger on the server restart.

This is what you asked in the first post and the automations concerned sunrise/sunset and an anchor light.

That’s what Muttley and I volunteered to help you with. Then you posted code that had nothing to do with the first three automations and that confused both Muttley and I and undoubtedly anyone else attempting to follow this topic.

Effectively, your solution post changed the example and wouldn’t be the way I would handle multiple triggers based on sunrise/sunset.

Sorry to hear the numeric_state example failed to work for you. I have used it and it works for me.

Final tip before I exit this topic, the service_template can be condensed to this:

"switch.turn_{{states('binary_sensor.excess_energy')}}"

Good luck.

word.

this is the first (and only relevant) thread coming up in searches for people searching for help simplifying what OP shows in his first post - the multiple automations needed for achieving such simple looking task like a ‘night light’ when doing it with the GUI

so indeed it would be nice if someone in the know could post a complete solution for such automation using the actual trigger events sunset/rise coming from what the GUI generates. The solution posted is using completely different triggers which a noob (like me) can’t adapt to the ‘sun’ platform (yet).

as a starter - this is what i got for turning a switch on 15 minutes before sunset:

- id: '1635274801609'
  alias: eFence
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -00:15
  condition: []
  action:
  - type: turn_on
    device_id: 5b6a82446b12bcdeadb625b19c311e96
    entity_id: switch.garden_plug_1
    domain: switch
  mode: single

Hi i am a neebie and trying to do this using the gui, i have done the following which seems to work but needs more testing and thought i would share:

Create new automation

2 sun triggers 1 at sunset and 1 at sunrise

in actions added a wait for time to pass and set it to 5 mins (to make sure it goes past sunset or sunrise).

2nd action if then
in if condition sun, before sunset and after sunrise, then turn the device off and in else turn the device on

Hope this helps others, if i find it doesnt work fully will remove.