Remember last sensor value instead of ‘unavailable’

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:

  1. created new helper input_number.n_power_day
  2. added automation to update that number
  3. created template (to show only INT values / avoid showing decimal numbers )
  4. 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 … :frowning:
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}}

Like this

- alias: Today energy Number
  trigger:
    platform: state    
    entity_id: sensor.energy_day_fronius_inverter_1_192_168_100_29  
  condition:
    condition: template
    value_template: "{{ states('sensor.energy_day_fronius_inverter_1_192_168_100_29') not in [ 'unavailable', 'unknown' ] }}"
  action:
    service: input_number.set_value
    entity_id: input_number.n_power_day
    data_template:
      value: "{{ trigger.to_state.state }}"
5 Likes

You may just want unknown. in which case:

- alias: Today energy Number
  trigger:
    platform: state    
    entity_id: sensor.energy_day_fronius_inverter_1_192_168_100_29  
  condition:
    condition: template
    value_template: "{{ states('sensor.energy_day_fronius_inverter_1_192_168_100_29') != 'unknown' }}"
  action:
    service: input_number.set_value
    entity_id: input_number.n_power_day
    data_template:
      value: "{{ trigger.to_state.state }}"

unavailable means there is probably something wrong with your sensor and you may want to see this.

2 Likes

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:
obraz

Thanks to your tip I can see always last known value - what I want to archive. :beers:

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 ?

2 Likes

@maciey Hi, did you find out how to reset it at midnight to 0?

I guess this should work, just set it up and will let it run to test.

image

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.

3 Likes

@Mithrox

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
1 Like

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…

2 Likes

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:

- id: "90060"
  alias: Fix unavailable data source
  trigger:
    - platform: state
      entity_id:
        - sensor.1
        - sensor.2
        [...]
        - sensor.n
  condition:
    - condition: template
      value_template: "{{ states(trigger.entity_id) in ['unavailable', 'unknown' ] }}"
  action:
    - service: update.sensor
      entity_id: trigger.entity_id
      data_template:
        value: "{{ trigger.from_state.state }}"

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.

1 Like