Automation with time, offset with an input_number

Hi everyone,
I’m struggling to make an automation where I turn on some lights after sunset, with an offset of some minutes. These would be selected quickly from an input_number, instead of going every time to change the automation. Link here the current automation and what I would do:

automation:
  #Luci ON tramonto
- alias: Luci ON al tramonto
  trigger:
    - platform: sun
      event: sunset
      offset: "00:15:00"
  condition:
    - condition: state
      entity_id: light.luce_led_rampa_garage
      state: "off"
  action:
    - service: light.turn_on
      data:
        entity_id: light.luce_led_rampa_garage

automation:
  #Luci ON tramonto
- alias: Luci ON al tramonto
  trigger:
    - platform: sun
      event: sunset
      offset: "input_number.turn_on_led_lights_garage_delay"
  condition:
    - condition: state
      entity_id: light.luce_led_rampa_garage
      state: "off"
  action:
    - service: light.turn_on
      data:
        entity_id: light.luce_led_rampa_garage

Is that possible?
Thanks

The Sunset Trigger’s offset option doesn’t support the use of an entity (like an input_number).

You will need to create a Template Sensor that calculates the desired sunset time (by adding the input_number’s value to the current day’s sunset time). Then your automation will use a Time Trigger that references the Template Sensor.

Any change you make to the input_number’s value will immediately change the Template Sensor’s computed sunset time (and affect when the automation triggers).

For example, the Template Sensor:

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: state
        entity_id: input_number.turn_on_led_lights_garage_delay
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: 'Sunset Offset'
        unique_id: 'sensor_sunset_offset'
        state: "{{ state_attr('sun.sun', 'next_setting') | as_datetime + timedelta(minutes= states('input_number.turn_on_led_lights_garage_delay')|int(0)) }}"
        device_class: timestamp

The automation:

automation:
- alias: Luci ON al tramonto
  trigger:
    - platform: time
      at: sensor.sunrise_offset
  condition:
    - condition: state
      entity_id: light.luce_led_rampa_garage
      state: "off"
  action:
    - service: light.turn_on
      target:
        entity_id: light.luce_led_rampa_garage

EDIT

Correction. Typo. Replaced entity_id with at in Time Trigger.

Yes it works, thank you so much for your help!

Only one thing on the automation for the ones who will read this in the future, sensor in automation was sensor.sunset_offset and you need to put at: not entity_id but yours was clearly a typo error (I bolded the line interested):

automation:
- alias: Luci ON al tramonto
  trigger:
    - platform: time
      **at: sensor.sunset_offset**
  condition:
    - condition: state
      entity_id: light.luce_led_rampa_garage
      state: "off"
  action:
    - service: light.turn_on
      target:
        entity_id: light.luce_led_rampa_garage

It works also with negative values, so it will turn on the lights before the sunset.

You’re welcome!

Thanks for reporting the typo; I have corrected the original example.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

Done, thanks again :slight_smile:

1 Like