Event not triggered?!

I want to turn a switch on/off if the sun rises/sets.

If I run it manually it just works! But if I wait for the sun.rise and sun.set events to get triggered, nothing happens… (I already enabled all entities of the “sun” service).

alias: Toggle corridor light based on sun
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: "00:00:03"
  - platform: sun
    event: sunset
    offset: "00:00:03"
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: above_horizon
        sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: switch.shelly_plug1_output
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: below_horizon
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.shelly_plug1_output
mode: single

Use trigger id’s to match trigger with the action. Because when working with offsets the state of the sun entity might not be what you expect.

Turning lights on and off at sunset and sunrise.

Thank you.

I have tried several code snippets and ended up with this (not working) script:

alias: toggleCorridorLight
description: Toggle corridor light based on sun
trigger:
  - id: "on"
    platform: sun
    event: sunset
  - id: "off"
    platform: sun
    event: sunrise
action:
  - target:
      entity_id: switch.shelly_plug1_output
    action: switch.turn_{{ trigger.id }}
mode: single

The trace says:
Error: Template rendered invalid service: switch.turn_

Don’t understand that since it seems that the configured trigger is correct. So why is there problem to use this trigger in the action expression?!

And:
There are no working triggers… even if I would fix the above issue: this script won’t execute hence no triggers will trigger…

If you ran the automation manually instead of waiting for sunrise/sunset, then there won’t be any trigger id because you bypassed the entire trigger section.

You’ll have to wait for the actual trigger event so that the trigger id is passed to your actions template.