Problem with setting template variable in Trigger

So how would I go about setting a template variable?

  • alias: Garage ON/OFF
    initial_state: True
    hide_entity: False
    trigger:
    • platform: sun
      event: sunrise
      {% set swaction=‘off’ %}
    • platform: sun
      event: sunset
      {% set swaction=‘on’ %}
      action:
      entity_id: switch.garage
      service: switch.turn_{{swaction}}

You template in the action, something like…

service_template: >
  {% if trigger.event == 'sunset' %}
    switch.turn_on
  {% else %}
    switch.turn_off
  {% endif %}

NB - you’ll have to check the docs for the syntax of getting the event, that was a guess.

I did consider this but I omitted that I have a ‘offset’ and I know I can check ‘trigger.offset’ but then if I change the ‘offset’ i’d have to also change the {% if trigger.offset==‘nn’ %} decision. Also since some offsets can be -ve then checking ‘trigger.event’ might work because the sunrise/sunset event hasn’t happened yet.

Yeah, sun is a pain in the backside.

The way I did this was to set an input boolean with an automation. When it’s ‘after dusk’ (sunset - offset) the boolean is on, when it’s ‘after dawn’ (sunrise + offset) the boolean is off.

Then I use the status of the boolean in my templates.

Thanks I’ll take a peek at that. Looks like we need to have ‘Trigger_Template’ and ‘Action_Template’.

In fairness there is a template trigger, but you’re in the same boat for offsets.