How can I get a trigger's attribute in a service template?

Trigger:

platform: sun
event: sunset
offset: '-00:30:00'

In service template I need something like: (simplified example)

{% if trigger.offset == '-00:30:00' %}
{% endif %}

I need a trigger’s offset by its ID, it’s not necessarily the trigger that triggered the automation (which has several).

No you don’t, sorry… I was to quick with my reply

Are you implying I can’t, or that you don’t know?

Maybe this:

{% if trigger.event.data.offset == '-00:30:00' %}
{% endif %}

I’ve never used it so I really don’t know if it will work

I meant that I gave the wrong answer and I don’t know the answer (yet, at least).
If it is possible, it should be in here:

Did a quick test by just dropping the trigger.offset to a persistent message and and it returns the offset exactly as set. In my case a positive offset. I don’t have time to check if it’s a time value or a string, but that’s a matter of trial and error. Just make multiple choose steps in your automation and see which catches :slight_smile:

1 Like

Sorry, I should have clarified. I need a trigger’s offset by its ID, it’s not necessarily the trigger that triggered the automation (which has several). Thanks very much for your help!

offset is represented as a timedelta object so try this:

{% if trigger.offset == timedelta(minutes=-30) %}
{% endif %}

If you have multiple triggers, you can identify them by their platform or id.

For example:

{% if trigger.id == 'my_id' and trigger.offset == timedelta(minutes=-30) %}
{% endif %}
{% if trigger.platform == 'sun' and trigger.offset == timedelta(minutes=-30) %}
{% endif %}
1 Like

I added an important clarification to my OP. Is it possible to get an entirely different trigger’s offset, by that trigger’s ID?

And can I use another trigger’s ID to get that trigger’s offset?

Post the triggers you are using and which trigger you want to reference.

Code is still a mess so here’s a simplified version:

{% if  trigger.id == 'homeassistant' %}
{{ get another trigger's (say 'sunset_custom_trigger') offset }}
{% endif %}

Post the triggers you are using, not the Jinja2 template you are trying to create.

Example:

trigger:
  - platform: homeassistant
    event: start
    id: ha
  - platform: sun
    event: sunrise
    offset: '-12:00:00'
    id: sunrise
  - platform: sun
    event: sunset
    offset: '-00:30:00'
    id: sunset

If trigger.id is ha, I want to access either trigger.sunset.offset or trigger.sunrise.offset.

You can’t do that. The trigger variable only contains information for the trigger that triggered the automation. If it was triggered by the first trigger (platform: homeassistant) then it doesn’t contain information for the other two triggers.

I knew this was coming. OK. Can I trigger a Python script in the service template or in any other way? I’ll just read everything I need there.

Yes but I am curious to know how you intend to make the python_script access information in the other two triggers.

I can read the contents of the YAML file can’t I? Yes, I know this is probably a bad idea. Is Python “sandboxed” when running in HA context?

I’m probably giving up on this automation anyway but I’d like to have the tools anyway.

Yes, it’s sandboxed; you can’t import modules.

I see. Well my goal with automation was sunrise-sunset based automation with state-persistence, kind of like Awesome HA Blueprint’s on-off one. But, with offset support.

So, “twenty minutes before sunrise” turn off the light, “1 hour before sunset turn on the lights”, and when HA restarts, check if you should turn on or turn off the lights, taking the offsets in consideration.

I realized quickly I couldn’t make a nice blueprint out of it, because I can’t use variables in sun.trigger offset, but now I guess even an automation will have to be written very inelegantly. I hope I’m missing something and will keep exploring…

(BTW: I know it’s “better” to use “elevation”)

Thanks @123.