Best practise for a time offset variable used in several automations

I have several automations which are turning on several lights when certain triggers happen. These automations have a after sunset condition with an offset in common:

condition:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: -00:20:00

At the moment I’m experimenting with finding a suitable offset value. Problem is, that I have to change it in every automation by hand.
So it would be nice to have one single point of truth for the offset value and all automations refer to this central variable(?).

What is the best way, to get this achieved?

Sorry, I posted a dumb suggestion to use an input_datetime. I think you want an input_number but not sure how to handle both positive and negative numbers as I wasn’t aware of an after_offset option.

Since you’re still in the experimental phase, you might want to consider switching from a time-based to solar elevation-based approach. The amount that solar illumination changes over a given period of time can be highly variable based on your latitude and the time of year. If you don’t have a direct means of determining illumination, you may find that elevation gives the “next best” results in terms of consistency.

I would also highly recommend the Sun2 integration, especially the Time at Elevation sensor which can be used in Time conditions and triggers and would give you a single point for your testing.

I will give the Sun2 integration a try in the near future.
But for the moment I want to stick with the time-based solution, because I want to learn, how to solve it with variables…

I created an input_number and set it to -20.
input_number.var_offset_beleuchtung_sonnenuntergang

But I don’t get it to work to put it in the right format.

This seams to work:

{{ '-' if states('input_number.var_offset_beleuchtung_sonnenuntergang') < '0' else '' }}{{'{:02d}:{:02d}:{:02d}'.format(0, states('input_number.var_offset_beleuchtung_sonnenuntergang') | int | abs, 0) }}

But when I use it in the automation like this:

alias: Kinobeleuchtung aktivieren
description: ""
trigger:
  <...>
condition:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: "{{ '-' if states('input_number.var_offset_beleuchtung_sonnenuntergang') < '0' else '' }}{{'{:02d}:{:02d}:{:02d}'.format(0, states('input_number.var_offset_beleuchtung_sonnenuntergang') | int | abs, 0) }}"
action:
  - service: scene.turn_on
    target:
      entity_id: scene.ha_szene_kinobeleuchtung_einschalten
    metadata: {}
mode: single

I get an error:


Message malformed: offset {{ '-' if states('input_number.var_offset_beleuchtung_sonnenuntergang') < '0' else '' }}{{'{:02d}:{:02d}:{:02d}'.format(0, states('input_number.var_offset_beleuchtung_sonnenuntergang') | int | abs, 0) }} should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['condition'][0]['after_offset']