Is it possible to trigger when time difference between current time and sensor state is greater than certain value?

hi guys, i am trying to keep track of presence of my raspberry pi. i had it running a curl script to post current time to my homeassistant instance which sets up a http sensor.
now i have a http sensor with state of time, eg. hh:mm:ss
how can i write an automation script to notify me if my pi is not active?
i wonder if there is a way to trigger when time difference between current time and sensor state is greater than certain value.

many thanks!

why not use a ping sensor?

because ping sensor is always making false alarms due to network problem.
I tried ping sensor and device_tracker, but sometimes it failed to find pi if i set scan_interval less than 90 seconds.
but i managed to update current time to my ha with 10 seconds interval.
I think it is because my pi is at a sub network behind ha which is at the same network with the router which pi connected to.

fix your network…

For whatever this is worth, I had something similar, it triggers when the last update to kitchen_motion sensor is more than 10 minutes (600 seconds). You can change last_updated to state for your sensor time state value.

- id: reset ipcam
  trigger:
  - platform: template
    value_template: '{% if (as_timestamp(now())-as_timestamp(states.sensor.kitchen_motion.last_updated)) > 600 %}true{% endif %}'

I don’t see how this works. This template trigger will be evaluated only when sensor.kitchen_motion’s state changes (either the state string or any of its attributes.) But, of course, when that happens, last_updated will be updated to now. So now - last_updated will never be more than 10 minutes. (In fact, it should always be about zero, or more accurately, the time from the state changing to the template trigger being evaluated due to that state change.)

I think it can be done, but let me understand … you have a script running on the same pi as HA that posts the current time to that HA instance. Is that right? What exactly are you trying to achieve with that? What exactly do you mean by “the presence of my pi”? Or are there two pi’s involved here?

You were right, that’s old test code I had.
That value_template has since modified into a condition paired with a time trigger.
Sorry for the confusion.

- id: netcat_check 
  trigger:   
  - platform: time
    minutes: "/3"
    seconds: 00
  condition:
    - condition: template
      value_template: '{% if (as_timestamp(now())-as_timestamp(states.sensor.lastnc.last_updated)) > 600 %}true{% endif %}'
1 Like

The classic (and simple) way of doing this is with a watchdog timer.

Example

  • A timer runs on HA and expires in 30 seconds.
  • The remote system is responsible for periodically resetting the watchdog timer every 15 seconds.
  • If it fails to reset the timer, the timer runs to expiration and then executes some ‘failure script’ (attempt to restart the remote-system, notify you that the remote-system failed, etc).

So the remote-system’s responsibility is to keep resetting the watchdog timer. By preventing the timer from expiring, the remote-system demonstrates it is alive and kicking. No date/time math is required.

2 Likes

sorry for the confusion.
i have a pi runing ha and other services with ip address of 192.168.4.x
i have another ha running in termux which is an android phone app with ip address of 192.168.1.x
192.168.4.x is a sub-network in 192.168.1.x
i want to use an android phone to notify if ha on pi is alive.
i managed to do it with JTPublic’s code on sceond post.
as far as i know, when pi stopped posting, last_updated value will remain unchanged and now - last_updated will be greater than like 20s.
i read the doc page and noticed that if i had a template trigger with now(), it will only be triggered to do the math when state of http sensor is changed. as a result, the first post of JTPublic’s code did not work for me.
many thanks.

Having one system post its current time to another system, and then using that on the second system to compare against its own current time is less than optimal, unless you’re doing something to sync their clocks automatically.

I agree with @123, you should use a watchdog type system. I.e., HA on the pi posts a timer.start service call to HA on the phone periodically. Then in HA on the phone, if the timer ever finishes use that as an indication that HA on the pi has died.

thank you for the idea.
this is certainly much better way. i will try later.

Ok,
here is my watchdog code:
in configration.yaml:

timer:
  pi_watchdog:
    duration: '00:00:20'

and in scripts.yaml

reset_timer:
  alias: reset timer
  sequence:
    - service: timer.cancel
      data:
        entity_id: timer.pi_watchdog
    - service: timer.start
      data:
        entity_id: timer.pi_watchdog
        duration: '00:00:20'

in automations.yaml

 - id: pi_watchdog_finish_alert_notify
  alias: pi_watchdog_finish_alert_notify
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.pi_watchdog
  condition: []
  action:
  - data:
      message: pi update failure 20s
      target: ****
      title: pi failure
    service: notify.le_alert_over
- id: pi_watchdog_reset
  alias: pi_watchdog_reset
  trigger:
  - platform: event
    event_type: state_changed
    event_data:
 #pi posts http sensor
      entity_id: sensor.rpi_heart
  condition: []
  action:
  - service: script.reset_timer

I got two wired things in timer component.

  1. timer.start does not reset the timer, so I had to use script to cancel first, then start.
  2. I set a duration of 20s, but when the timer starts, I saw 26s, 21s, 20s at the frontend. And I tested it by stopping pi from posting to let the timer finish. The timer ran for 26s. It is very wired!

That should not be necessary. The timer.start service will restart a timer if it’s already running. All you should have to do is:

service: timer.start
entity_id: timer.pi_watchdog

I can’t explain that. That has certainly not been my experience.

Also, why are you still having the pi cause a state change, and then have that state change restart the timer? Why not just have the pi use the call service API to directly call the timer/restart service, passing in the entity_id of timer.pi_watchdog?

I am not familiar with using the call service API.
could you give some reference or an example to start with?

the version of my ha is 0.80.3, and it is not reseting the timer.
I tried call service from frontend or using a script to do that and it is not working.

How do you get HA on the pi to change a state of a sensor in HA on the phone? Aren’t you already using this API? You’d use the same API but just change the details of the URL and the body of the POST.

How do you know it’s not working? You can’t look at “remaining time”, at least as best I can tell. You have to call timer.start and then manually time it to know if it’s working. If it’s not, then you have bigger problems.

Hey,
I read the docs and finished my configuration with your suggestion. thank you!

Here is what I do,
I replaced the script of posting to a http sensor with a service call using REST api.
I deleted the automation of state_change.
I use an input_boolean to control the state of pi so that when it is back, I get noticed.
I use timer.cancel as event trigger to set that state to on.
On the other hand, I called timer.start on homeassistant.start trigger to check if pi is alive.
here is my automation.yaml

- id: '1540560585135'
  alias: ha_start_alert_notify
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      message: LE-HA is up
      target: **
      title: notice
    service: notify.le_alert_over
  - data:
      entity_id: timer.pi_watchdog
    service: timer.start
- id: pi_watchdog_finish_alert_notify
  alias: pi_watchdog_finish_alert_notify
  trigger:
  - event_data:
      entity_id: timer.pi_watchdog
    event_type: timer.finished
    platform: event
  condition: []
  action:
  - data:
      message: pi update failure
      target: **
      title: warning
    service: notify.le_alert_over
  - data:
      entity_id: input_boolean.pi_check
    service: input_boolean.turn_off
- id: '1541121488417'
  alias: pi_resume_notify
  trigger:
  - event_data:
      entity_id: timer.pi_watchdog
    event_type: timer.cancelled
    platform: event
  condition:
  - condition: state
    entity_id: input_boolean.pi_check
    state: 'off'
  action:
  - data:
      message: pi is back
      target: **
      title: Notice
    service: notify.le_alert_over
  - data:
      entity_id: input_boolean.pi_check
    service: input_boolean.turn_on

for others information, https://developers.home-assistant.io/docs/en/external_api_rest.html