How to turn on with sunset and turn off with sunrise

Tried this but it doesn’t work:

alias: Global - Renna
description: ""
trigger:
  - platform: sun
    event: sunrise
  - platform: sun
    event: sunset
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.platform == 'sun' and trigger.event == 'sunset') }}"
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.sonoff_presa_smart
            data: {}
      - conditions:
          - condition: template
            value_template: "{{ trigger.platform == 'sun' and trigger.event == 'sunrise') }}"
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.sonoff_presa_smart
            data: {}
    default: []
mode: single
{{ states ('sun.sun' == 'below_horizon' }}
{{ states ('sun.sun' == 'above_horizon' }}

Invalid template:

alias: Global - Renna
description: ""
trigger:
  - platform: sun
    event: sunrise
  - platform: sun
    event: sunset
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states ('sun.sun' == 'below_horizon' }}"
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.sonoff_presa_smart
            data: {}
      - conditions:
          - condition: template
            value_template: "{{ states ('sun.sun' == 'above_horizon' }}"
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.sonoff_presa_smart
            data: {}
    default: []
mode: single
{{ states('sun.sun' == 'below_horizon') }}
{{ states('sun.sun' == 'above_horizon') }}
alias: Global - Renna
description: ""
trigger:
  - id: 'on'
    platform: sun
    event: sunrise
  - id: 'off'
    platform: sun
    event: sunset
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.sonoff_presa_smart
mode: single
2 Likes

Yep…my bad…bracket wrong

{{ states('sun.sun') == 'below_horizon' }}

EDIT: as often usual … @123 has great short versions :slight_smile:

And to riff on Taras’ very compact solution:

alias: Global - Renna
description: ""
trigger:
  - platform: state
    entity_id: sun.sun
    to: ~
action:
  - service: "switch.turn_{{ 'off' if is_state('sun.sun','above_horizon') else 'on' }}"
    target:
      entity_id: switch.sonoff_presa_smart
mode: single
1 Like

Thanks, seems working

And a slightly shorter riff on Tinkerer’s riff :slightly_smiling_face:

alias: Global - Renna
description: ""
trigger:
  - platform: state
    entity_id: sun.sun
    to: ~
action:
  - service: "switch.turn_{{ iif('above' in trigger.to_state.state, 'off', 'on') }}"
    target:
      entity_id: switch.sonoff_presa_smart
mode: single
1 Like