Sunrise turn-on and Sunset turn off automation

I am trying to set up an aquarium light to turn off at sunset. this is the code I came up with, but it doesn’t seem to be working. I also need to turn on the light at 0800hrs in the morning, for which I might need a second automation script.

alias: 10gal aquarium lights off
description: Sunset light off
trigger:
  - platform: state
    entity_id: switch.sonoff_1000cf6gh8
    to: 'off'
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: switch.turn_off
    entity_id: switch.sonoff_1000cf6gh8
mode: single

Use sunset/sunrise as the trigger

alias: 10gal aquarium lights off
description: Sunset light off
trigger:
  - platform: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: switch.turn_off
    entity_id: switch.sonoff_1000cf6gh8
mode: single

meaning I should change state to sunset?

No like I posted sun.sun is a sensor for sunrise sunset. It’s two states are above_horizon “sunrise” or below_horizon “sunset”. If you go to dev tools you can find the state for sensor, switch, etc which you need to use in automations for them to work.

You mean Sun - Home Assistant

That is sun.sun you don’t need to add sun: to your config, it’s part of the default config. Sun is the friendly name, sun.sun is how HA understands it. Here it is in developer tools.

I tried but it give this error

Message malformed: extra keys not allowed @ data['state']

Sorry about that should be to

alias: 10gal aquarium lights off
description: Sunset light off
trigger:
  - platform: state
    entity_id: sun.sun
    to: below_horizon
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_1000cf6gh8
mode: single
1 Like

the problem with this automation is, it doesn’t respond to the state of the sun, e.g right now the sun’s state is above horizon and if I click on run automation it still turns off :yum: :crazy_face:

As it should because when you manually execute an automation, it skips the trigger and condition (if any) and simply executes the action.

This behavior is documented in Automations - Troubleshooting.

Please note that if you click on Trigger of an automation in the frontend, only the action part will be executed by Home Assistant. That means you can’t test your trigger or condition part that way. It also means that if your automation uses some data from triggers, it won’t work properly as well just because trigger is not defined in this scenario.

You should review the documentation’s Automation chapter because you don’t appear to understand how triggers and conditions operate. If you are trying to do this:

then you don’t use a State Trigger based on a switch entity. You can use a State Trigger based on the sun.sun entity or simply use a Sun Trigger.

Here’s an example of an automation that turns on the switch at 08:00 and off at sunset.

alias: 10gal aquarium lights
description: On at 8AM and off at sunset
trigger:
  - platform: time
    at: '08:00:00'
  - platform: sun
    event: sunset
action:
  - service: "switch.turn_{{ 'on' if trigger.platform == 'time' else 'off' }}"
    entity_id: switch.sonoff_1000cf6gh8
mode: single
1 Like

:point_right: "switch.turn_{{ 'on' if trigger.platform == 'time' else 'off' }}" :+1:

That’s slick

Is it possible to make switch turn on with sunset and turn off at specified time in single automation?

alias: example
trigger:
  - platform: time
    at: '06:00:00'
  - platform: sun
    event: sunset
action:
  - service: "switch.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}"
    entity_id: switch.your_switch
mode: single

Oh so @123 this is the logic. You make two actions which automation runs in single day and in every action you let it decide what to do based on condition. Is this the proper way how to construct any automations? Because I usualy make two separated for similar thing. Thank you.

If I want to use this for front and rear porch lights where all front are 1 group and all rear are 1 group would it be something like this?

alias: Front & Rear Porch Lights
trigger:
  - platform: time
    at: '06:00:00'
  - platform: sun
    event: sunset
action:
  - service: "light.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}"
    entity_id: light.front_porch_lights
  - service: "light.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}"
    entity_id: light.rear_porch_lights
mode: single

I think this is correct. I’m not entirely sure how to test this without just waiting for the sun to go down and 6:00 AM the following morning. Appreciate you posting that. First time I’ve tried doing this. YAML is so weird sometimes.

Thank you

You can also do it like this:

alias: Front & Rear Porch Lights
trigger:
  - platform: time
    at: '06:00:00'
  - platform: sun
    event: sunset
action:
  - service: "light.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}"
    target:
      entity_id:
        - light.front_porch_lights
        - light.rear_porch_lights
mode: single
2 Likes

hi @123,

thank you for your help.

is this possible?:

on at sunset and off at sunrise?

work this:

  - alias: 'Licht Sunset Sunrise'
    trigger:
    - platform: sun
      event: sunset
      offset: '+00:10:00'
    - platform: sun
      event: sunrise
      offset: '+00:10:00'
    action:
    - service: "light.turn_{{'on' if trigger.event == 'sunset' else 'off'}}"
      target:
        entity_id:
          - light.ess_led
          - light.tv_indirekt_led
          - light.led_leiste

Thank You!

Yes. Using trigger.event to determine which one of the two Sun Triggers triggered is the right way to do it.

1 Like