Help with templating/using a variable sunset offset

I have some lighting automations which use a negative offset to the sunset time, and then a random delay, to turn my lights on at some random time which occurs at latest sunset. They work fine, but as the seasons change, I find that I want the offset number to be larger or smaller, and I’m tired of editing yaml files when I want the change. So I’m trying to use an input number to set it from the front end. But I can’t get my automation code to validate. I have this as my input number:

  name: Sunset Offset
  icon: mdi:clock
  min: 0
  max: 59
  step: 1

and this as my automation:

- alias: Turn on couch lights near sunset
  hide_entity: false
    platform: sun
    event: sunset
    offset: '-00:{{ states('input_number.sunset_offset') | int }}:00'
    condition: state
    entity_id: light.couch_lights
    state: 'off'
    - delay: '00:{{ range(0,15) | random | int }}:00'
    - service: light.turn_on
      entity_id: light.couch_lights
        brightness: 129

The template works in the dev tools template checker. If I use single quotes around the offset as shown, I get an error about a scalar:

Error: Testing configuration at /config
ERROR:homeassistant.util.yaml.loader:while parsing a block mapping
  in "/config/automations.yaml", line 16, column 5
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 18, column 29
Failed config
  General Errors: 
    - Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/automations.yaml", line 16, column 5
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 18, column 29

It’s definitely the sunset offset causing the error. If I use double quotes, or no quotes, I just get a huge flood of errors. What am I doing wrong?

You can’t use a template with the offset parameter of the sun trigger. You’ll have to come up with a different way to do this. I’m sure there are plenty of topics in the forum that have solutions, or suggestions that you could use. It just takes a bit of searching.

1 Like

Thank you for that information. I was thinking that offset would be similar to delay, but apparently not. I’m sure I can come up with an alternate method; this was just the first thing that popped into my head. Off to put on my thinking cap… :grin:

You’re not the first one to have either assumed the offset parameter would accept a template, or to ask for that to be implemented. I’ve even considered implementing it myself. (I added template support to the for options of several triggers.) So it’s a reasonable expectation. It just hasn’t been done.

Typically, though, the idea is to use a sun elevation trigger instead. This is even suggested in the docs.

Thanks, I’ll have a play with sun elevation and see how that works for me. Thanks for all your contributions to HA. I had no idea when I started to play with it (about 1 1/2 years ago) that it would turn into such a hobby, and how much I would learn about so many different things :joy:

You’re welcome. I started with it about the same time, and I totally agree. :slight_smile:

1 Like

Old topic but I came across it today because like a lot of people it seems, I tried to use Offset like Delay with the Sunset.

To get around it, I left the Sun trigger without an offset and simply added an actual DELAY step before the step I originally wanted to call. Works for me.


Correct me if I’m wrong but this isn’t going to work if the offset should be negative…?