Show last value of a sensor that is not equal to "Unavailable"?

I am using IOT Link to collect information of windows computers, like drive space ect.
But it is only showing the stats when computer is on. I want to see the last value of a sensor when the computer is offline, like “Last value, besides Unavailable”. I will also make this persist trough reboots of Home-Assistant.

How can this be done? I guess with a template sensor ect.
example:
image

How can this be done?

you will have to create a template sensor for each one using the following (rough) syntax:

{% if states('sensor.your_real_sensor') == 'unavailable' %}
  states('sensor.your_template_sensor')
{% else %}
  states('sensor.your_real_sensor')
{% endif %}

but what to input in sensor.your_template_sensor?

what ever you decide to call the template sensor.

sensor:
  - platform: template
    sensors:
      your_template_sensor: # <-- whatever you call it here...
        value_template: >
          <code from above here>

if the real sensor is unavailable it will just return its own current value as its new value.

Like this?

sensor:
  - platform: template
    sensors:
      your_template_sensor: # <-- whatever you call it here...
        value_template: >
          {% if states('sensor.your_real_sensor') == 'unavailable' %}
            states('sensor.your_template_sensor')
          {% else %}
            states('sensor.your_real_sensor')
          {% endif %}

I can try.

looks right to me.

Of course you need to put in your real entity_ids. And what ever other options you want (name, unit of measure, etc.)

I got this to work, well, until I reboot Home-Assistant and the real sensor are unavailable, then the template sensor is showing “Unknown”

      flemming_laptop_storage_c_total_free_space_last_value:
        icon_template: mdi:harddisk
        friendly_name: Ledig lagringskapaistet
        unit_of_measurement: "GB"
        value_template: >
          {% if states('sensor.flemming_laptop_storage_c_total_free_space') == 'unavailable' %}
            {{ states('sensor.flemming_laptop_storage_c_total_free_space_last_value') }}
          {% else %}
            {{ states('sensor.flemming_laptop_storage_c_total_free_space') }}
          {% endif %}

I want this template value to be stored between home-assistant reboots

When you configured IOT Link, did you follow the instructions and do this?

Under mqtt:messages: set retain: to true.

I did. But I guess that is just storing the message until home-assistant is online if it was offline before the computer.
It is not helping if:

  • PC are on, MQTT up, Home-Assistant up
  • PC are turned off
  • Home-Assistant (and MQTT server) reboots when PC are still off

There’s something wrong with your configuration because a retained message is stored on the broker so it doesn’t matter if the broker restarts or a client disconnects/reconnects, the stored message will continue to be available.

Running the add-on for Mosquitto broker. Current version: 6.0.

Can you spot any mistakes?
Log: 1635311778: New connection from 172.30.32.2 on port 1883.1635311778: Socket er - Pastebin.com

config:

logins:
  - username: mqtt
    password: mqtt
customize:
  active: false
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false
anonymous: false

Network:

Network
Container	Host	Description
1883/tcp	
1883
Normal MQTT
1884/tcp	
1884
MQTT over WebSocket
8883/tcp	
8883
Normal MQTT with SSL
8884/tcp	
8884
MQTT over WebSocket with SSL

It’s not the broker’s configuration that is suspect, it’s IOT Link’s configuration that may be the problem.

That’s not how a broker handles retained messages.

If a topic’s message is not retained when published, only the subscribers connected to the broker receive it. The next subsciber to connect to the broker doesn’t automatically receive whatever was recently published to the topic.

If a topic’s message is retained when published, subscribers connected to the broker receive it and any future subscribers will also receive it at the moment they connect to the broker. The topic’s message is stored by the broker permanently and disappears only if purged (restarting the broker doesn’t purge retained messages).

For more information, refer to this explanation: