I put below in my template.yaml, but it never seems to trigger (value always stays unknown).
- trigger:
- platform: state
entity_id: binary_sensor.freezer_door_sensor_2_contact
from: 'on'
to: 'off'
sensor:
- name: Freezer door open last time
state: "{{ states.binary_sensor.fridge_door_sensor_2_contact.last_changed }}"
# Last time freezer was opened
- trigger:
- platform: state
entity_id: binary_sensor.freezer_door_sensor_1_contact
from: 'on'
to: 'off'
sensor:
- name: Freezer door open last time
state: "{{ states.binary_sensor.freezer_door_sensor_1_contact.last_changed }}"
# Last time fridge was opened
- trigger:
- platform: state
entity_id: binary_sensor.fridge_door_sensor_1_contact
from: 'on'
to: 'off'
sensor:
- name: Fridge door open last time
state: "{{ states.binary_sensor.fridge_door_sensor_1_contact.last_changed }}"
I’ll restart now and see if that changes anything. I’m guessing the syntax otherwise is ok and it should work ?
One thing i don’t understand is how the trigger part is part of this sensor. How does HA know this? There is no alias/name or anything. Before i used triggers i would (like with many other sensors) just use sensor: so that would indicate the start of a new sensor stanza.
I used this in my automation to respond to these template values:
service: notify.alexa_media_atv_s_family_hub
data:
message: >-
The fridge was opened {{ ( (as_timestamp(now()) -
as_timestamp(states.sensor.fridge_door_open_last_time)) / 3600
)| round(2) }} hours ago
data:
type: tts
method: all
enabled: true
But i keep getting whenever i run this:
Executed: 11 January 2023, 08:50:11
Error: Error rendering data template: ValueError: Template error: as_timestamp got invalid input '<template TemplateState(<state sensor.fridge_door_open_last_time=2023-01-11 08:47:30.003108+00:00; friendly_name=Fridge door open last time @ 2023-01-11T08:47:30.005767+00:00>)>' when rendering template 'The fridge was opened {{ ( (as_timestamp(now()) - as_timestamp(states.sensor.fridge_door_open_last_time | default(0))) / 3600 )| round(2) }} hours ago' but no default was specified
I tried putting a |default(0) everywhere but it doesn’t seem to like it, no matter where i put it. Maybe i need to put the default(0) in the actual template?
Not sure what you are asking. Can you re-phrase it?
Change the template to this:
data:
message: >-
The fridge was opened {{ ( (as_timestamp(now()) -
as_timestamp(states('sensor.fridge_door_open_last_time')|float(0)) / 3600
)| round(2) }} hours ago
Assuming this entity exists: sensor.fridge_door_open_last_time and is a datetime object.
I mean, how does HA know what trigger is part of what sensor. As i have many sensors in my template.yaml, and some of them don’t have triggers. Where does the ‘sensor’ part start (assuming the trigger is part of the sensor i define).
If i use the above code, I get message malformed; template value needs to be a string
That sensor does exist:
sensor:
- name: Fridge door open last time
state: "{{ states.binary_sensor.fridge_door_sensor_1_contact.last_changed }}"
As a sidenote, not directly related to this; if i define a template_value based upon a sensor, if that sensor does not exist when restarting HA, the template value never gets created correct? It doesn’t just come to alive when the sensor has become active? (that’s my understanding currently anyway, from what i can see).
State based (not triggered) template sensors update whenever there is a change to any of the entities in the template. This is done automatically by the home assistant template sensor integration.
In the above example, the trigger: and sensor: variable of Trigger 1 and Sensor 1 are in the same list (as defined by the far left dash) so the value of Sensor 1 will only be rendered when Trigger 1 fires.
Sensor 2 is in it’s own “stanza” without a trigger, so it will be rendered any time any entity in it’s template updates.
Triggers 2A and 2B apply to both Sensor 3 and Binary Sensor 3. Each of those sensor will be rendered whenever either of the triggers fire.
Note: I have added line breaks to separate the “stanzas”, but they are not necessary.