Binary Sensor last known state when unavailable

Hi, im new to HA, have been adding some sensors successfully, have just tried powering an eps8266 with a Lipo, and am using the ‘deep sleep’ mode to save power. When the esp goes to sleep, the state shows as ‘unavailable’… is there a way to display the ‘last known’ state (of a binary sensor) on my default view?
The idea is to see where the sensor was at last time of checking (I have it set to check for 10s every 10mins).
appreciate any help ,thanks.

Why not have the sensor post an MQTT payload?

Then HASS can simply collect that from the MQTT broker (which can be the running on the same machine as HASS) when and if it changes.

Or, if you don’t use MQTT and don’t want to go through the effort of getting that set up, you can use an automation that records the state of the sensor whenever it changes (except for when it changes to ‘unavailable’) to an appropriate input_xxx entity and display that entity in the frontend.

input_text:
  my_sensor_most_recent:
    name: Last known value of sensor.my_sensor

automation:
  - alias: Record state of sensor.my_sensor
    trigger:
      platform: state
      entity_id: sensor.my_sensor
    condition:
      condition: template
      value_template: >
        {{ trigger.to_state.state not in [trigger.from_state.state, 'unavailable'] }}
    action:
      service: input_text.set_value
      entity_id: input_text.my_sensor_most_recent
      data_template:
        value: "{{ trigger.to_state.state }}"
2 Likes

Hi thanks for the replies, yes I am using MQTT, however the payload goes to unavailable as soon as the chip powers down, is there a way to display last known from there? Im currently simply displaying the entity value on my front end (as per below). I don’t have any automations running off it yet, wanted to get the notification working first.

I tried the second suggestion, but couldn’t get the input text entity to work.
I appreciate the help, as I said I’m very green…

automations.yaml…

  • alias: Record state of binary_sensor.garage_door_tilt
    trigger:
    platform: state
    entity_id: binary_sensor.garage_door_tilt
    condition:
    condition: template
    value_template: >
    {{ trigger.to_state.state not in [trigger.from_state.state, ‘unavailable’] }}
    action:
    service: input_text.set_value
    entity_id: input_text.my_sensor_most_recent
    data_template:
    value: “{{ trigger.to_state.state }}”

config.yaml…
input_text:
my_sensor_most_recent:
name: Last known value of binary_sensor.garage_door_tilt

groups.yaml…
Garage:
name: Garage
entities:
- binary_sensor.garage_door_tilt
- input_text.my_sensor_most_recent

thanks heaps!

Please format your code properly. YAML is very sensitive to indenting. The easiest way to format your code is to select it all and click the </> button. Or, you can put a line before and after your code, where each of these lines contain only three back-tick characters. The back-tick character looks like this: `

I didn’t actually test the code, but it’s based on things I’ve done before. I might have overlooked something. When you say it didn’t work, what do you mean by that exactly? Were there any error messages?

I fixed it, it didn’t like the ‘name’ I had set, so it works now thanks!
it does however reply with ‘on’ and ‘off’ as last known, which I get is from the binary sensor, is there a way to alias that as ‘Open’ and ‘Closed’ ?
thanks in advance, this is my learning curve :smile:

value: "{{ 'Open' if trigger.to_state.state == 'on' else 'Closed' }}"

Thanks so much, that worked perfectly!
Can i do the same thing in a notification?
what i want to do is poll this value at 10pm each night and report the door position in a notification, but i would prefer ‘Closed’ to ‘Off’.
cheers

Of course. Just make sure to use data_template instead of data.

Hello,

I tried applying this solution to my problem, but it’s not working properly:

configuration.yaml entry:
#…
recorder:
#…
input_text:
wohnzimmer_temperatur_most_recent:
name: Last known value of sensor.wohnzimmer_temperatur

automations.yaml entry:
- alias: Record state of sensor.wohnzimmer_temperatur
trigger:
platform: state
entity_id: sensor.wohnzimmer_temperatur
condition:
condition: template
value_template: >
{{ trigger.to_state.state not in [trigger.from_state.state, ‘unavailable’] }}
action:
service: input_text.set_value
entity_id: input_text.wohnzimmer_temperatur_most_recent
data_template:
value: “{{ trigger.to_state.state }}”

How do I get input_text to know the last known state of my sensor.wohnzimmer_temperatur? I supposed via the record: component, but I’m missing the point to connect both components.

BTW: The </> Button seems not to work for me?

Did you get this working? If not, then you really need to format it correctly so we can read it properly. If the “preformatted text” button is not working for you, then try entering a line before the text which contains exactly three “back tick” characters, and enter another line just like it after the code, like this:

```
xxx: yyy
```

You may need to re-copy the code directly from a text editor first.

Thanks for the reply. Actually I did not get this working. Thus, my sensors are working 24/7 now.
Thanks for the tip with the back tick chars.

We are more then a year later now, is there an easy way to show last known state of an sensor? We have a Tesla and it goes in to sleep a lot (to save battery). I don’t want to wake up the car just to see how much juice is in the battery. When the car is in sleep it shows ‘unknown’ at the range sensor. In the history you can see the latest state, but i would love to see the that in my lovelace dashboard. Any easy tips…?

I’m not aware of an easier way, so my previous suggestion should still work, although in your case you might want to change the condition’s value_template to:

      value_template: >
        {{ trigger.to_state.state not in
           [trigger.from_state.state, 'unknown', 'unavailable'] }}

Ok thanks. I will try this. If i understand correct i can make an automation through the ui and use these examples?
I am trying to better understand this option. Will it overrule the latest state of the sensor? Or will it skip/ignore states there are not in the condition ‘unknown, etc’
And this will be another entity right? So it will live besided the original entity/sensor?

I don’t use the UI to create / modify automations, so I can’t answer that question.

The suggestion I referenced creates a new input_text entity, and uses an automation to change the value of that new entity to match the value of a sensor whenever that sensor changes, except that it ignores certain values. If you’d rather not use an input_text entity in the UI, then you could create a template sensor that follows the state of the input_text entity, which displays nicer in the UI, and also doesn’t allow the user to change its value.

Thanks, i understand. Will try that!

Thank you !!!