Tsar
(Christian)
February 1, 2025, 8:14am
1
Hi,
I created this sensor :
- name: "Voorgevel avond"
unique_id: "voorgevel_avond"
state: >
{{ state_attr('sun.sun', 'next_setting') | as_datetime + timedelta(minutes = states('input_number.number_offset_voorgevel_avond')|int(0)) }}
device_class: timestamp
Based on that sensor I created an automation :
triggers:
- at: sensor.voorgevel_avond
id: inschakelen
trigger: time
- at: input_datetime.tijdstip_uitschakelen_verlichting_voorgevel
id: uitschakelen
trigger: time
This works fine, except if home assistant was just started before…(other date).
I could solve this by triggering a template :
{{ as_timestamp(now().strftime('%Y-%m-%d %H:%M:00')) >= as_timestamp(today_at(as_timestamp(states('sensor.voorgevel_avond') | as_datetime, 0)| timestamp_custom('%H:%M:%S', true))) }}
Or by changing my sensor as this :
{{ as_timestamp(state_attr('sun.sun', 'next_setting') | as_datetime + timedelta(minutes = states('input_number.number_offset_voorgevel_avond')|int(0)))| timestamp_custom('%H:%M:%S', true) }}
…triggering by this template :
{{ as_timestamp(now().strftime('%Y-%m-%d %H:%M:%S')) >= as_timestamp(today_at(states("sensor.voorgevel_avond"))) }}
I was wondering if this could be done a shorter way ?
Tsar:
Voorgevel avond
Can you descripe in english what you are trying to do?
It seems you want to switch something on/off at a time that is related to sunrise/sunset?
Tsar
(Christian)
February 1, 2025, 12:20pm
3
Yes, indeed.
Based on sunrise/sunset with an offset (some minutes before or after) I switch on/off some lights.
Are you sure you need a template for that? The sun integration natively supports triggering an automation with an offset:
triggers:
- trigger: sun
event: sunrise
offset: "-01:00:01"
1 Like
This is the way to do it, at least for none or negative offsets. Not sure whether positive offsets will work as you will then be shooting for a moving goalpost. (When you reach sunrise/sunset, the datetime of the next sunrise/sunset will immediately move forward by one day.)
Positive offsets should work just as well too. The link I provided in my earlier post makes it pretty clear that both positive and negative offsets are supported.
Tsar
(Christian)
February 2, 2025, 6:47am
7
What am I missing here ?
- trigger:
- trigger: time
at: "00:00:00"
- trigger: event
event_type: event_template_reloaded
- trigger: homeassistant
event: start
- trigger: state
entity_id:
- input_number.number_offset_voorgevel_avond
sensor:
- name: "Voorgevel avond"
unique_id: "voorgevel_avond"
state: >
{{ as_timestamp(state_attr('sun.sun', 'next_setting') | as_datetime + timedelta(minutes = states('input_number.number_offset_voorgevel_avond')|int(0)))| timestamp_custom('%H:%M:%S', true) }}
device_class: timestamp
Logger: homeassistant.components.sensor.helpers
Source: components/sensor/helpers.py:23
integration: Sensor (documentation, issues)
First occurred: 07:44:25 (1 occurrences)
Last logged: 07:44:25
sensor.voorgevel_avond rendered invalid timestamp: 17:56:06
I would assume device_class: timestamp
expects a timestamp, or at the very least a datetime or datetime string. You are feeding it a time string only.