ashscott
(Ash)
January 27, 2025, 8:01pm
1
I need a sensor to show the last available value of a sensor so that when the sensor is not available I can display the last value;
So far, I have this, but it shows ‘unknown’ when the sensor is not available.
{% if states('sensor.458_battery_monitor') != 'unavailable' %}
{{ states('sensor.458_battery_monitor) }}
{% else %}
{{ states('sensor.battery_voltage_backup') | default(states('sensor.458_battery_monitor').last_updated_value) }}
{% endif %}
When the sensor is available it returns the current value OK.
How ca I access the last value of the sensor from the recorder, I assume, to use in the template sensor?
Any ideas? Is this possible?
petro
(Petro)
January 27, 2025, 8:11pm
2
{% if has_value('sensor.458_battery_monitor') %}
{{ states('sensor.458_battery_monitor') }}
{% else %}
{{ this.state }}
{% endif %}
if you want it to persist over restarts you have to make a trigger based template entity.
- trigger:
- trigger: state
entity_id: sensor.458_battery_monitor
not_to:
- unknown
- unavailable
sensor:
- name: 458_battery_monitor 2
state: '{{ trigger.to_state.state }}'
ashscott
(Ash)
January 27, 2025, 8:36pm
3
Thank you, Petro.
I’ve tried this but get the error “‘this’ is undefined”.
What is ‘this.state’?
I’m assuming that the below is a storage of the most recent value.
- trigger:
- trigger: state
entity_id: sensor.458_battery_monitor
not_to:
- unknown
- unavailable
sensor:
- name: 458_battery_monitor 2
state: '{{ trigger.to_state.state }}'
In which case I just change {{ this.state }} to {{ 458_battery_monitor 2.state }}.
Is that correct?
petro
(Petro)
January 27, 2025, 8:40pm
4
that won’t work in the template editor, only in your entity. this.state is the current state.
it’s just an entity that represents the latest state that is available.
ashscott
(Ash)
January 27, 2025, 8:58pm
5
Here’s where I’m at, but the created sensors aren’t available. I have reloaded the template entities. Does it need a full restart?
- sensor:
- name:
unique_id: stored_458_battery_voltage
state: >
{% if has_value('sensor.458_battery_monitor') %}
{{ states('sensor.458_battery_monitor') }}
{% else %}
{{ sensor.458_battery_monitor_voltage_store }}
{% endif %}
- template:
- trigger:
- trigger: state
entity_id: sensor.458_battery_monitor_458_battery_voltage
not_to:
- unknown
- unavailable
sensor:
- name: 458_battery_monitor_voltage_store
state: "{{ trigger.to_state.state }}"
petro
(Petro)
January 27, 2025, 9:00pm
6
- sensor:
- name:
unique_id: stored_458_battery_voltage
state: >
{% if has_value('sensor.458_battery_monitor') %}
{{ states('sensor.458_battery_monitor') }}
{% else %}
{{ this.state }}
{% endif %}
- trigger:
- trigger: state
entity_id: sensor.458_battery_monitor_458_battery_voltage
not_to:
- unknown
- unavailable
sensor:
- name: 458_battery_monitor_voltage_store
state: "{{ trigger.to_state.state }}"
ashscott
(Ash)
January 27, 2025, 9:36pm
7
Brilliant!
Thank you for your help and time. I appreciate it.
virtualj
(Virtualj)
March 6, 2025, 12:37am
8
I have a very similar issue, but I don’t understand how can I apply to an automation without creating a new sensor. I think it should be very simple to get the last valid value of a sensor… But it isn’t!
this is what I did:
alias: Svuotamento Serbatoio
description: ""
triggers:
- trigger: state
entity_id:
- sensor.esymini_troiano_fct_total_delivered_flow_mc
not_to:
- unknown
- unavailable
conditions: []
actions:
- action: input_number.set_value
target:
entity_id: input_number.livello_serbatoio
data:
value: >-
{{ max(states('input_number.livello_serbatoio') |
float(states('input_number.capacita_massima_serbatoio') | int) - (
trigger.to_state.state | float - trigger.from_state.state | float ) *
1000, 0) | round(2) }}
- action: automation.turn_on
metadata: {}
data: {}
target:
entity_id: automation.riempimento_serbatoio
mode: single
I would manage the case where trigger.from_state is not available giving a “default” value in float(default) where default should be the last sensor valid value.
Is it possible?
d921
(dominic)
March 6, 2025, 12:56am
9
Look carefully! The solution from @petro was not an automation, even though it has triggers. It is a trigger-based sensor template .
virtualj
(Virtualj)
March 6, 2025, 1:34am
10
Yes I saw, but I want to get the last valid value inside the automation without creating a duplicate sensor of the original only to fix the unavailable states.
d921
(dominic)
March 6, 2025, 1:36am
11
Creating a new sensor for that purpose is the most straightforward way to go about it on the Home Assistant platform, even if it feels like there should be a more minimalist way.
There is not, to my knowledge, a way to access prior states of a sensor from within a script or automation, at least not without introducing major new headaches that would dwarf the inconvenience of a new sensor.
My card reads from abstract_weight, but after a HA reboot, it shows up as NaN until I step on the weighing machine again.
I tried the trigger, but wasn’t able to get it to work, can help me with this please? Thank you.
template:
- sensor:
- name: abstract_weight
unique_id: abstract_weight
icon: mdi:scale-bathroom
unit_of_measurement: kg
device_class: weight
state: >
{% set weight = states('sensor.from_hardware') | float %}
{{ weight if weight >= 50 else this.state }}
availability: "{{ has_value('sensor.from_hardware') }}"