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