Alaric
December 30, 2022, 11:23am
1
Hi,
I know this is probably a simple question, but can’t get it right…
I am monitoring RPI’s using MQTT and want to show the “last time seen” (as this is a property in the MQTT message) even if the RPi is down. It the should show the last date/time it was seen (persistent). (of course this value will be gone after a reboot) , capturing the last heartbeat date/time stamp.
octpi_last_seen: # Custom dummy, Will keep the last seen date / time#
friendly_name: "Heartbeat"
value_template: "{{ is_state(octopi_ls_dummy) }}"
icon_template: mdi:heart-pulse
octopi_ls_dummy:
value_template: >
{% if not state_attr('sensor.rpi_monitor_octopi', 'timestamp') == "Unavailable" %}
{% set octpi_last_seen = as_timestamp(state_attr('sensor.rpi_monitor_octopi', 'timestamp')) | timestamp_custom('%d-%m-%Y %H:%M') %}
{% endif %}
The idea is that, when the RPi, goes down, the sensor shows the last date/time it was seen, instead of “unavailable”, in order to be able troubleshooting more accurate.
This is the reason for trying it with a “dummy” sensor to be updated when the condition is true.
thx in advance
finity
December 30, 2022, 3:09pm
2
try this:
octpi_last_seen: # Custom dummy, Will keep the last seen date / time#
friendly_name: "Heartbeat"
value_template: "{{ states('sensor.octopi_ls_dummy') }}"
icon_template: mdi:heart-pulse
octopi_ls_dummy:
value_template: >
{% if not state_attr('sensor.rpi_monitor_octopi', 'timestamp') == "Unavailable" %}
{{ state_attr('sensor.rpi_monitor_octopi', 'timestamp') | timestamp_custom('%d-%m-%Y %H:%M') }}
{% else %}
{{ states('sensor.octopi_ls_dummy') }}
{% endif %}
Alaric
December 30, 2022, 4:04pm
3
Hi,
Thanks for the reply. Unfortunately it does not work. Bothe sensors give “unavailable”
I changed a small piece to see if that would work without any luck
octopi_last_seen: # Custom dummy, Will keep the last seen date / time#
friendly_name: "Heartbeat"
value_template: >
{{ states('sensor.octopi_ls_dummy') }}
icon_template: mdi:heart-pulse
octopi_ls_dummy:
value_template: >
{% if not state('sensor.rpi_monitor_octopi','unavailable') %}
{{ state_attr('sensor.rpi_monitor_octopi', 'timestamp') | timestamp_custom('%d-%m-%Y %H:%M') }}
{% else %}
{{ states('sensor.octopi_ls_dummy') }}
{% endif %}
finity
December 30, 2022, 5:38pm
4
if you post the following in the dev tools templates tab what does it show?
{{ state('sensor.rpi_monitor_octopi') }}
{{ state_attr('sensor.rpi_monitor_octopi', 'timestamp') }}
{{ state_attr('sensor.rpi_monitor_octopi', 'timestamp') | timestamp_custom('%d-%m-%Y %H:%M') }}
also there is now a new thing wrong with the current template:
{% if not state('sensor.rpi_monitor_octopi','unavailable') %}
that won’t work.
at a minimum it needs to be this:
{% if not is_state('sensor.rpi_monitor_octopi','unavailable') %}
without the info above I can’t know what to tell you to try next.
Alaric
December 30, 2022, 6:24pm
5
Hi,
{{ state_attr('sensor.rpi_monitor_octopi', 'timestamp') }}
{{ as_timestamp(state_attr('sensor.rpi_monitor_octopi', 'timestamp')) | timestamp_custom('%d-%m-%Y %H:%M') }}
Ouputs:
2022-12-30T19:17:19+01:00
30-12-2022 19:17
{{ state('sensor.rpi_monitor_octopi') }}
Fails due to (i guess) the the json output. See attached
Indeed my bad. and changed it to
octopi_last_seen: # Custom dummy, Will keep the last seen date / time#
friendly_name: "Heartbeat"
value_template: >
{{ states('sensor.octopi_ls_dummy') }}
icon_template: mdi:heart-pulse
octopi_ls_dummy:
value_template: >
{% if not is_state('sensor.rpi_monitor_octopi','unavailable') %}
{{ state_attr('sensor.rpi_monitor_octopi', 'timestamp') | timestamp_custom('%d-%m-%Y %H:%M') }}
{% else %}
{{ states('sensor.octopi_ls_dummy') }}
{% endif %}
finity
December 30, 2022, 6:31pm
6
Sorry, I didn’t realize the timestamp attribute was in a datetime format and not a unix float timestamp.
try this (added back in the as_timestamp part from your original):
octopi_last_seen: # Custom dummy, Will keep the last seen date / time#
friendly_name: "Heartbeat"
value_template: >
{{ states('sensor.octopi_ls_dummy') }}
icon_template: mdi:heart-pulse
octopi_ls_dummy:
value_template: >
{% if not is_state('sensor.rpi_monitor_octopi','unavailable') %}
{{ as_timestamp(state_attr('sensor.rpi_monitor_octopi', 'timestamp')) | timestamp_custom('%d-%m-%Y %H:%M') }}
{% else %}
{{ states('sensor.octopi_ls_dummy') }}
{% endif %}
Alaric
December 30, 2022, 6:53pm
7
finity:
octopi_last_seen: # Custom dummy, Will keep the last seen date / time#
friendly_name: "Heartbeat"
value_template: >
{{ states('sensor.octopi_ls_dummy') }}
icon_template: mdi:heart-pulse
octopi_ls_dummy:
value_template: >
{% if not is_state('sensor.rpi_monitor_octopi','unavailable') %}
{{ as_timestamp(state_attr('sensor.rpi_monitor_octopi', 'timestamp')) | timestamp_custom('%d-%m-%Y %H:%M') }}
{% else %}
{{ states('sensor.octopi_ls_dummy') }}
{% endif %}
Hi,
That works. see att. When shutting down the RPI monitor daemon.
Since bot sensors keep the value, seems that one of them is obsolete?
Deamon up
Daemon down
1 Like