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.
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.
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.
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.
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.
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.
timer.start does not reset the timer, so I had to use script to cancel first, then start.
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