I have mqtt sensors, and I want to monitor their state.
I have binary sensor for ping
sensor:
- platform: ping
name: 'FrontYard'
host: 192.168.0.101
scan_interval: 1
Occasionally sensor answers to ping, but does not send updates. And for that I check if value has been updated for 5 minutes using template
{{ (as_timestamp(now())-as_timestamp(states.sensor.frontyard.last_updated)) > 300}}
That can’t either be used as only source of truth, for temperature might stay same for given time.
Hence I have tried to create binary_sensor template combining these two:
sensor:
frontyard_disconnected:
friendly_name: "Front yard sensor not updating"
value_template: >-
{{ (as_timestamp(now())-as_timestamp(states.sensor.frontyard.last_updated)) > 300
and is_state('binary_sensor.frontyard', 'off')}}
This validates ok, and when I disconnect sensor, and try that clause on developer tools -> templates, I do get True after 5 minutes. And obviously both clauses of template give True at that time.
But problem is, that binary sensor does not switch to On state.
I would like to use binary sensor to be able to create alert based on state change.
Now I have automation, which works, but I don’t like it (on principle, for it must run unneeded cycles. Not that I would not have resources to do that):
automation:
- alias: sensor.frontyard_notupdating
trigger:
- platform: time_pattern
minutes: '/10'
condition:
condition: and
conditions:
- condition: state
entity_id: binary_sensor.frontyard
state: 'off'
- condition: template
value_template: '{{ (as_timestamp(now())-as_timestamp(states.sensor.frontyard.last_updated)) > 300 }}'
action:
service: notify.telegram
data:
message: 'Front yard not updating'
Any ideas why my binary sensor does not work? I guess I could do 3 binary sensors: for ping, for updating value, and third checking state of two earlier ones. But still feels wrong. On principle.