I want to show last known sensor value (today energy production)
Ive read best bet is use for that helpers / input_number (so kind of global variable if I understood that well)
So what I did is:
created new helper input_number.n_power_day
added automation to update that number
created template (to show only INT values / avoid showing decimal numbers )
display on levelace sensor from template
And this is generally working, however if sensor is unavailable (eg device is disconnected from WIFI) n_power_day variable is set in unavailable state.
I think this can be resolved by adding some condition to automation (to update n_power_day value only if number > 0 or such), tried but cant really find good syntax …
Can someone please help me ?
Below code Im using
AUTOMATION
- alias: Today energy Number
trigger:
platform: state
entity_id: sensor.energy_day_fronius_inverter_1_192_168_100_29
action:
service: input_number.set_value
entity_id: input_number.n_power_day
data_template:
value: "{{ trigger.to_state.state }}"
TEMPLATE
- name: Current Day
state: >
{{states("input_number.n_power_day") |int}}
Actually Id like to show this number no matter what is happening with this device.
(I have other monitors showing me device is not accesible)
So your 1st tip was great! Thanks!
In this case it is Fronius inverter, which is going to sleep on sunset and becomes accessible via WIFI untl sunrise
Here is how I can see readings of standard Fronius sensor coming from integration in such case:
Thanks to your tip I can see always last known value - what I want to archive.
Maybe last question if you are so kind helping - how I can reset this to ZERO every new day?
Can I use automation triggered on certain time (eg 00:00:00) and set value to zero ?
In my opinion it would be really good to turn use cases like this into a feature request. What about to add an option to a sensor’s configuration like "Use last know value in case of “unavailable”/“unknown” state. Example Use Case: I have a smart power plug, which is connected to my vacuum cleaner, which obviously is not always “online”. To still use the measured energy values for energy consumption calculations it would be useful to have the option to filter “unknow”/“unavailable” states and just use the last valid value of measurement.
Here is how I did it.
Template + automation - see below
EDIT
This might be better tip actually:
template:
- sensor:
# avooid 'unknown'
- name: Current Power
state: '{{ states("sensor.power_photovoltaics_fronius_power_flow_0_192_168_0_129") | int (default=0) }}'
# taken from helper
- name: Current Day
state: '{{ states("input_number.n_power_day") |int(default=0) }}'
AUTOMATION:
alias: Power Day MAX
description: ""
trigger:
- platform: state
entity_id:
- sensor.energy_day_fronius_inverter_1_192_168_0_129
condition:
- condition: template
value_template: >-
{{ states('sensor.energy_day_fronius_inverter_1_192_168_0_129') not in [
'unavailable', 'unknown' ] }}
- condition: template
value_template: >-
{{ states('sensor.energy_day_fronius_inverter_1_192_168_0_129') >
states("input_number.n_power_day_max") }}
action:
- service: input_number.set_value
entity_id: input_number.n_power_day_max
data_template:
value: "{{ trigger.to_state.state |round|int }}"
mode: single
Yes, I would like the sensor to say “33 degrees (2 hours ago)” or similar. This should really be a standard feature.
It’s not that I mind a sensor saying “unavailable” (not that much anyway). But often the senor fails completely and I get doing errors showing up in lovelace - at that point you can be sure the WAF is gone…
I’m facing a similar issue and fully agree we should find a way to correct those outliers. I’ve got some Qubino smart meters, an ESP to monitor my electrical consumption, and some integration to get the consumption of my heaters (Overkiz). Now the issue is that if any of them goes banana, all the graphs are crippled, and my long-term statistics are also failing.
I couldn’t find any service to update a sensor. Still, the concept behind this would be to detect if a sensor is becoming unavailable or unknown and then set the sensor value to its previous value. This could be generalized to all sensors you want, using an automation similar to this I guess:
I know there is no “update.sensor” service or whatever the name should be, but I’m sure you get the point.
Or, even better indeed, being able to set a flag on an entity that states that if it becomes unknown or unavailable, it should retain the previous value.
One way would be to store the value in a templated sensor and update it every X min, only if the sensor’s value is not “unknown” or “unavailable” otherwise, keep previous value.
Btw, this code only updates my templated sensor every 5 minutes with the trigger (and not every time channel_a_power changes, which could be several times per minute and crowd the database).
But @tom_l solution of triggering an automation only based on state change is cool too. I guess it depends if your sensor changes its value very often.
<TL/DR> if your sensor changes often, I’d probably template it, otherwise the automation of Tom triggering on state change is clean.
thx @tom_l great news for the condition system!
I’m not sure to get the update part, so if I set a trigger every 5 min, the templated sensor still gets updated everytime any of its component change ? if so I probably didn’4t understand the point of it…?