"last-changed" secondary info to ignore "unavailable" state

I have a feeling there’s no easy way of doing this but thought I’d check as it’s getting increasingly annoying to see!

Is there a way to exclude a certain state from the “last-changed” secondary info? My main dashboard lists my contact, door and motion sensors which I have secondary-info set to last-changed. The problem is sometimes deconz will lose connection to either the sensors or HA (not sure which but only very briefly - seconds) but that means all the last-changed times then reset. I know technically they did “change” but for me it’s not that useful as it’s no longer really clear when something did properly change - like a leak, new motion or a window opened. Annoyingly the same applies when HA is restarted.

Is this possible without making a looooad of manual sensors in the config? Would be good to have the option to have both (current last-changed and a revised one) - especially with the new release making it so simple to set in the UI.

Thanks!

2 Likes

Quick bump to this question as I have the same one.

I use last_changed for one of my sensors in a wait template for one of my automations. Unfortunately if that sensor goes unavailable at all it messes with that template. I really want a change to unavailable and back to be ignored in this case (and again, I have a whole bunch of these sensors and automations, so I’d rather not make a ton of template sensors or the like to work around this)

Still the biggest and only real flaw in HA for me :frowning: There’s a similar thread with many more comments than mine from long ago and I don’t think there’s much interest in it sadly.

1 Like

Maybe investigate why DeConz is being buggy instead of the work around? If all DeConz sensors go unavailable at the same time it’s most likely to be DeConz itself rather than Zigbee because the report needs of Zigbee are pretty low.

I guess my original comment is slightly out of date now as I don’t really have Deconz connection issues now!

Same issue applies though when restarting HA… I want to know what time a door was last opened on my dashboard, not the last time HA was restarted next to my door entities. I can see why it does it as they change state during restart but it’s not really useful to do so, for me at least.

1 Like

So for anyone who would like an answer to this:

Create a binary sensor that checks if a certain entity has it’s state changed to unavailable, then you can check the last_changed value of this binary sensor to ensure your last_changed of the original entity wasn’t to the unavailable state.

So a binary sensor like this would work:

sensor_unavailable:
        friendly_name: "[Binary Sensor] Sensor has been unavailable"
        value_template: >-
          {{is_state('binary_sensor.sensor_opened', 'unavailable')}}
        unique_id: "Sensor_Unavailable"

Edit:

And to reference and check if the sensor was unavailable in the last x minutes you can use the following line in a conditional:

(as_timestamp(now()) - as_timestamp(states.binary_sensor.sensor_unavailable.last_changed) < x*60

Thanks and I’ve seen a few approaches to this but I think the point is more WHY isn’t this standard out the box as it’s a seemingly common problem. I have too many physical sensors to be creating loads of binary sensors!

2 Likes

You can use a mushroom-template-card (mushroom card) to achieve this functionality easily. In the secondary information section, paste:

{% if is_state("switch.coffee_machine", "on") %}
{{ relative_time(states.switch.coffee_machine.last_changed)}} 
{% else %}
{% endif %}

This will display the time since switched on, or nothing if the switch is off.

The full yaml definition is:

type: custom:mushroom-template-card
primary: Coffee Machine
secondary: >-
  {% if is_state("switch.coffee_machine", "on") %}
  {{relative_time(states.switch.coffee_machine.last_changed)}}
  {% else %}
  {% endif %}
icon: mdi:coffee
entity: switch.coffee_machine
icon_color: >-
  {% set entity_state=states(entity) %}
  {% if entity_state=='on'%}
  accent
  {% else %}
  grey
  {% endif %}
tap_action:
  action: toggle

Off:
image

On:
image

Hi Reggleston, this was more due to the actual behavior of last-changed rather than displaying it. It’s a commonly requested feature but sadly it’s still an issue. More info including work arounds here : Persistent version of "last-changed" for the UI? - #114 by petro

1 Like