Binary Sensor for timestamp Device Class

I am trying to setup a binary sensor to trigger an alert if an mqtt sensor I have does not check in every four hours. The time sensor is setup and looks like this on my dashboard.

Annotation%202019-11-14%20102412-1
Which would seem quite perfect as it displays how long ago the sensor checked in with HA. But of course this is not how it is displayed under my states. It just gives me the raw date and time.

My question of course is how do I best use this to create a binary sensor which I then can take to trigger an alert? Obviously, in my perfect non-coding world it would be easiest to do something like this below but that clearly won’t work. Do I need to convert the raw timestamp somehow or what is the best way to attack this? Any help would be greatly appreciate, thanks.

  - platform: template
    sensors:
      mail_box_sensor_lastboot_over4hours:
        value_template: "{% if states('sensor.water_heater_alarm_last_boot_time') > 4 minutes}
        on
        {% else %}
        off
        {%endif %}"
        friendly_name: '4 hour Test'

That’s how I would do it. Here’s one possible approach:

binary_sensor:
  - platform: template
    sensors:
      mail_box_sensor_lastboot_over4hours:
        value_template: >-
          {{ (as_timestamp(now()) - as_timestamp(states('sensor.water_heater_alarm_last_boot_time'))) > 4*60*60 }}
        friendly_name: '4 hour Test'

Converts both times to timestamps (seconds), calculates the difference (in seconds) and compares to 4 hours. Your code is using 4 minutes, but the names say 4 hours, so adjust the comparison if you actually want 4 minutes.

You want a binary_sensor template to return True or False, not a string.

FYI, the dashboard time probably comes from the relative_time function or something like it. I wouldn’t consider using that in a binary_sensor template though, since it’s just a string and can switch between units, “5 minutes”, “1 hour”, etc.

I really appreciate the quick response on this and it half works. It seems to only trigger when the sensor is on and the MQTT timestamp comes through or I reboot HA or call and entity update service. Any idea on how to get the entity to update automatically?

Here is my current code testing every thirty seconds.

  - platform: template
    sensors:
      mail_box_sensor_lastboot_over4hours:
        friendly_name: '4 hour Test'
        entity_id: 
         - sensor.water_heater_alarm_last_boot_time
        value_template: >-
          {{ (as_timestamp(now()) - as_timestamp(states('sensor.water_heater_alarm_last_boot_time'))) > 30 }}

It seems to only trigger when the sensor is on and the MQTT timestamp comes through or I reboot HA or call and entity update service. Any idea on how to get the entity to update automatically?

I’m not really sure which entities are what here, the template or the entity being tracked. Are you saying the template sensor never updates unless you restart?

This template sensor will only be re-evaluated when the state of sensor.water_heater_alarm_last_boot_time changes. If sensor.water_heater_alarm_last_boot_time does not change, then neither will this template.

The use of entity_id in the template isn’t really necessary in this case. HA will automatically detect that it is being used because of the states() function in the value. It certainly doesn’t hurt though.

What I am trying to do is see if the sensor.water_heater_alarm_last_boot_time stops checking in with HA. So I guess that would remain static unless it check in but I need to compare that to the current time which should be constantly changing.

I found the below post which talks about “the function now() is not an entity; i.e., it does not cause state_change events.” Which I think may be the problem.

So I’ve been trying to tweak the code to use something like (as_timestamp('sensor.date_time') with no luck.

I found the below post which talks about “the function now() is not an entity; i.e., it does not cause state_change events.” Which I think may be the problem.

Ah you’re right, I missed that. That’s correct, now() will not trigger a template update, and if the boot time entity also doesn’t change, nothing will ever be updated. So you basically already have the right ideas, you either need to use the date time sensor in the templates entity_id which will force the template to re-evaluate because of time changing, or write an automation that triggers by a time interval and the action is to call the update entity service.

Converting sensor.date_time to a timestamp should also work, but I think your code is wrong: as_timestamp(states('sensor.date_time')) is probably what you need. Also make sure you’ve actually enabled it in your configuration.yaml.

- platform: template
    sensors:
      mail_box_sensor_lastboot_over4hours:
        friendly_name: '4 hour Test'
        value_template: >-
          {{ (as_timestamp(states('sensor.date_time')) - as_timestamp(states('sensor.water_heater_alarm_last_boot_time'))) > 30 }}

Or

 - platform: template
    sensors:
      mail_box_sensor_lastboot_over4hours:
        friendly_name: '4 hour Test'
        entity_id: 
         - sensor.date_time
        value_template: >-
          {{ (as_timestamp(now()) - as_timestamp(states('sensor.water_heater_alarm_last_boot_time'))) > 30 }}

freshcoast - Thank you so much for your time and patience with me, it appears to be working like I need it to now. The solution that worked for me was the following for all those in the future.

- platform: template
    sensors:
      mail_box_sensor_lastboot_over4hours:
        friendly_name: '4 hour Test'
        entity_id: 
         - sensor.date_time
        value_template: >-
          {{ (as_timestamp(now()) - as_timestamp(states('sensor.water_heater_alarm_last_boot_time'))) > 30 }}