Update dummy sensor with other sensor value

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

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 %}

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 %}

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.

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 %}

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 %}

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