ashscott
(Ash)
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)
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)
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)
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)
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)
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)
7
Brilliant!
Thank you for your help and time. I appreciate it.