When I try to use this sensor in an automation (e.g. at: wake_up_lights_richard_time_starts), it doesn’t work (value is 14:31:00 for example). I assume that it is being seen as a string or incorrect somehow; but stumped as to how I can correct this. Any help would be appreciated
As stated in the docs, your sensor needs to be assigned a timestamp device class.
Templates must be enclosed in quotes or prefaced by a multi-line quote symbol ( > or |).
Make sure you include the entity’s domain in the at: of your time trigger:
trigger:
- platform: time
at: sensor.wake_up_lights_richard_time_starts
FWIW, if you are starting out you should get used to using the current format for Template sensors. The legacy format you have used will work, but it lacks some of the abilities that the newer format offers and it is no longer the recommended method.
template:
- sensor:
- name: wake_up_lights_richard_time_starts
icon: mdi:alarm-on
device_class: timestamp
state: >
{% set time = states('input_datetime.wake_up_lights_richard_set_time') | as_datetime | as_local %}
{% set pre_min = states('input_number.wake_up_lights_start_richard_minutes') | int(0) %}
{{ time - timedelta(minutes=pre_min) }}
Drew - thanks for this. I’ve copied your code into my configuration.yaml file, it checks as valid code, but returns an error on restart as follows:
Invalid config for [template]: [value_template] is an invalid option for [template]. Check: template->sensor->0->value_template. (See /config/configuration.yaml, line 230).
Line 224 is the first line “template:”
Found there was an extra " ’ ", in the line time = states(‘input_datetime.wake_up_lights_richard_set_time’’) and removed it.
In the logs, still shows an error. Pasted the code into Developer Tools->Template and it returns the following error:
AttributeError: ‘NoneType’ object has no attribute 'tzinfo’
As mentioned, I’m new to HA and don’t fully understand this.
Did try various options with the sensor assigned as timestamp class, but hadn’t realised the current format (had used some previous automations from others, clearly a bit older)
I have corrected the cause of the errors in the post above. The current format uses state: not value_template… I missed it when converting your original.
Drew - you are a legend! Many thanks for the help. It now makes much more sense to me and I’m slowly getting to grips with how HA code works. Still a long way to go, but this was a massive help. Works perfectly!