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:
sunset_offset:
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
trigger:
platform: sun
event: sunset
offset: '-00:{{ states('input_number.sunset_offset') | int }}:00'
condition:
condition: state
entity_id: light.couch_lights
state: 'off'
action:
- delay: '00:{{ range(0,15) | random | int }}:00'
- service: light.turn_on
entity_id: light.couch_lights
data:
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?