Sun.sun sunrise and sun set automation

I have been using a an automation using a light sensor to trigger some mqtt lights in the yard. Over time I have noticed that my lights sensor is not reliable, and to make this short. I would like to use the sun.sun entity as a trigger, but I have not been successful. below is my automation. any help would be much appreciated.

Here is what I have.
,

  • id: ‘1552021957044’
    alias: Sunset
    trigger:
    • event: sunset
      platform: sun
      condition:
    • below: ‘-0.15’
      condition: numeric_state
      entity_id: sun.sun
      action:
    • data:
      entity_id: group.yardlights
      service: homeassistant.turn_on
  • id: ‘1552024074663’
    alias: Sunrise
    trigger:
    • event: sunrise
      platform: sun
      condition:
    • above: ‘0.63’
      condition: numeric_state
      entity_id: sun.sun
      action:
    • data:
      entity_id: group.yardlights
      service: homeassistant.turn_on
      ,

Can you use that blue bar on top of the page to format your code pls?
Can you also briefly explain what each automation is for and what you expect to see out of it?

Sorry but I am highlighting text and using the code block button but nothing.

The Sunset automation is to trigger the yard lights group to turn on when the sun.sun sensor is at sunset. The Sunrise automation is to turn the lights off when the sensor show sunrise.

Thanks, Without the code correctly formatted I can’t really help as I can’t see if there are indentation issues which yaml is really strict on.
If you’re working on a real keyboard (not mobile device) you can enclose your code with 3 backticks like this:
```
code
```

Don’t know if that’s right, but it did something.

what are you trying to do with this condition?

I think this can never work. The automation will only be triggered once (When the sun has set) and if the condition is evaluated false then, nothing happenes even if the condition comes true later on.

I am not married to the position. I could live with sunset, or if it could detect above and below horizon.
I have used the automation without the condition, and manually triggered without any success.

Ok, not it is getting difficult as it might be a formating error.
Can you please repost your config but add ``` in the line before and after? (Thats 3 backticks)

If you want to trigger on sunset, leave out the condition:

- id: '1552021957044'
  alias: Yard Lights on at Sunset
  trigger:
    platform: sun
    event: sunset
  action:
    service: homeassistant.turn_on
    entity_id: group.yardlights

OK here are the automation without the conditions

- id: '1552021957044'
  alias: Sunset
  trigger:
  - event: sunset
    platform: sun
  action:
  - data:
      entity_id: group.yardlights
    service: homeassistant.turn_on

- id: '1552024074663'
  alias: Sunrise
  trigger:
  - event: sunrise
    platform: sun
  action:
  - data:
      entity_id: group.yardlights
    service: homeassistant.turn_off
    ```

So you want the lights to turn on at sunset AND sunrise?

- id: '1552021957044'
  alias: Yard Lights on at Sunset
  trigger:
    - platform: sun
      event: sunset
    - platform: sun
      event: sunrise
  action:
    service: homeassistant.turn_on
    entity_id: group.yardlights

Fixed that last one. Off at sunrise.

can you manually trigger the automation?

You can do it in two automations, or one with the use of templates:

- id: '1552021957044'
  alias: Yard Lights
  trigger:
    - platform: sun
      event: sunset
    - platform: sun
      event: sunrise
  action:
    service_template: >-
      {% if trigger.event == 'sunset' %}
        homeassistant.turn_on
      {% elif trigger.event == 'sunrise' %}
        homeassistant.turn_off
      {% endif %}
    entity_id: group.yardlights
5 Likes

Yes, they manually work now.

Thanks for the assist, everybody.

Hi, my automation was working fine then with some update, it stoped working. My automations.yaml looks like this:

  • alias: SonoffSwitch Lights Auto
    trigger:

    • platform: sun
      event: sunset
      offset: “00:30:00”
    • platform: sun
      event: sunrise
      offset: “-00:30:00”
      action:
      service_template: >
      {% if trigger.event == “sunset” %}
      homeassistant.turn_on
      {% elif trigger.event == “sunrise” %}
      homeassistant.turn_off
      {% endif %}

    entity_id: switch.sonoff_switch_01

And the entry in my configuration.yaml looks like this:
automation: !include automations.yaml
script: !include scripts.yaml

mqtt:
broker: core-mosquitto
username: amir
password: **********

switch:
platform: mqtt
name: “Sonoff Switch 01”
command_topic: “cmnd/sonoff1/power”
state_topic: “stat/sonoff1/POWER”
qos: 1
payload_on: “ON”
payload_off: “OFF”
retain: true

You should use switch.turn_on and switch.turn_off as the services.

1 Like