Use trigger.offset

Hi everybody, I would like to retrieve the defined value offset in trigger and use it as a transition value in action. Here is what I have defined in my automation

  - platform: sun
    event: sunset
    offset: '-00:15:00'
  - condition: state
    entity_id: group.persons
    state: 'home'
  - service: scene.turn_on
    entity_id: scene.lecture_salon
      #transition: 900
      transition: "{{ trigger.offset.total_seconds() }}"

when the trigger is triggered, I think the value is at 0 and not at the set value, how to do?

thanks :slight_smile:

The offset is not templateable or variable so just use the same numeric value for your transition.

The documentation indicates that trigger.offset is accessible to a template.

Sun Trigger

It’s a timedelta object and total_seconds() is the correct method. Perhaps it produces a float and transition only accepts integer? Try converting the result to integer with the int filter.

But what’s the point?

This isn’t going to change:

offset: '-00:15:00'

Convenience. If the user later chooses to change the offset value, there’s no need to modify anything in the template, because it refers to trigger.offset.

I was wrong about float/integer. Transition accepts a float value. You could try re-arranging it like this but I doubt it’s the cause of the problem:

  - service: scene.turn_on
      entity_id: scene.lecture_salon
      transition: "{{ trigger.offset.total_seconds() }}"

Thanks for your help guy’s, placing entity_id after data_template doesn’t change anything. In fact, the value obtained is a negative value, it should be possible to indicate that it is a positive value. I guess a Python function must exist for that.

abs() returns the absolute value (unsigned), you can use it as a filter:

transition: "{{ trigger.offset.total_seconds()|abs }}"

Perfect, with filter int !

 transition: "{{ trigger.offset.total_seconds()|abs|int }}"