Problem with binary sensors

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.

The template sensor will only update on a change of the entity_id, not on time.
You need an automation that triggers every min and a condition that checks the entity_id state as well as when it was last updated, then run your action
So what you’ve done above is right and the correct way to do it…

Ok, thank you for the clarification!

In other template sensor I use delay, which works fine, based on that sensor I get alarm if amplifier is left on for prolonged period.
Got to think if I could somehow use delay to reduce amount of automations. Not that it really matters :slight_smile:

sensor:
  -platform: template
    amplifier_on:
      friendly_name: "Amplifier on"
      delay_on:
        hours: 3
      value_template: "{{ (states('sensor.amplifier')|float) > (states('sensor.livingroom')|float + 2) }}"

So, I did figure out solution, but it is not simple, and effectively does not reduce load, but I got what I want. Bunch of code, and explanation afterwards.

binary_sensor:
  - platform: ping
    name: 'Frontyard'
    host: 192.168.0.101
    scan_interval: 60
  - platform: template
    sensors:
      frontyard_disconnected:
        friendly_name: "Front yard not updating"
        value_template: >-
          {{ is_state('input_boolean.frontyard_ok', 'off')
             and is_state('binary_sensor.frontyard', 'off')}}

input_boolean:
  frontyard_ok:
    name: Frontyard disconnected
    initial: on

sensor:
  - platform: template
    sensors:
      etupiha_updatetime:
        unit_of_measurement: 'second'
        value_template: >-
          {{ ((as_timestamp(states('sensor.date_time_iso'))-as_timestamp(states.sensor.etupiha.last_updated)) | int)}}

automation:
 - alias: sensor.frontyard_notupdating
   trigger:
     - platform: numeric_state
       entity_id: sensor.frontyard_updatetime
       above: 1800
   condition:
     condition: state
     entity_id: binary_sensor.frontyard
     state: 'off'
   action:
     - service: persistent_notification.create
       data:
         message: 'Front yard not updating'
         title: frontyard sensor
         notification_id: frontyard
     - service: input_boolean.turn_off
       entity_id: input_boolean.frontyard_ok
 
 - alias: sensor.frontyard_restored
   trigger:
     - platform: state
       entity_id: binary_sensor.frontyard
       from: 'off'
       to: 'on'
       for: "00:05:00"
   condition:
     condition: numeric_state
     entity_id: sensor.frontyard_updatetime
     below: 300
   action:
     - service: persistent_notification.dismiss
       data:
         notification_id: frontyard
     - service: input_boolean.turn_on
       entity_id: input_boolean.frontyard_ok

alert:
  frontyard_disconnected:
    name: Frontyard disconnected
    done_message: Frontyard sensor ok
    repeat: 60
    state: 'on'
    entity_id: binary_sensor.frontyard_disconnected
    notifiers:
      - telegram

First basics: binary sensor for ping, and binary template sensor which is used for firing alert.
Input boolean is for tracking state, and for Lovelace UI component. Its state if controlled by automation.

Heart of this is sensor, which tracks update interval. It turns out (from documentation) that now() cant be used as part of template sensor, thereefore had to use date_time_iso. And due to sensor value update intervals, frontyard_updatetime is negative from time to time (practically 0 to -60) which does not matter.
So when sensor stops updating, automation is triggered to generate alert and ui components.
And when sensor once more answers to ping, second automation clears situtation.

As additional component to persistent notification and telegram messagei I wanted Lovelace UI component to show the situation.
I bring notification via combination of conditional card and glance.
All ok:
image

Problem:
image

I wanted that in normal situation there is no field visible at all. Entity filter would be easier, but it creates narrow panel even if no entity matches filter. This way I get the look I want. Though it creates awfully long UI configuration, when you start adding up the sensors.

Card:

cards:
  - card:
      entities:
        - input_boolean.water_running
      show_icon: false
      show_name: false
      show_state: false
      theme: material_dark_red_alert
      title: Water running!
      type: glance
    conditions:
      - entity: input_boolean.water_running
        state: 'on'
    type: conditional
  - card:
      entities:
        - binary_sensor.amplifier_on
      show_icon: false
      show_name: false
      show_state: false
      theme: material_dark_red_alert
      title: Amplifier on
      type: glance
    conditions:
      - entity: binary_sensor.amplifier_on
        state: 'on'
    type: conditional
  - card:
      entities:
        - input_boolean.frontyard_ok
      show_icon: false
      show_name: false
      show_state: false
      theme: material_dark_red_alert
      title: Front yard sensor not working
      type: glance
    conditions:
      - entity: input_boolean.frontyard_ok
        state: 'off'
    type: conditional
  - card:
      entities:
        - input_boolean.backyard_ok
      show_icon: false
      show_name: false
      show_state: false
      theme: material_dark_red_alert
      title: Backyard sensor not working
      type: glance
    conditions:
      - entity: input_boolean.backyard_ok
        state: 'off'
    type: conditional
  - cards:
      - entity: sensor.time
        show_name: false
        type: sensor
      - entity: sensor.date_template
        type: sensor
        name: Date
    type: horizontal-stack
type: vertical-stack