How to use a sensor's last known good value?

Hi folks. I rely on cloud weather forecast sensors to run my heating. When they become unavailable, I’d like to use their ‘last known good’ data instead. So I think I’m asking -

How do I make a sensor to mirror the latest available data of another sensor?

Make sense? Thanks in advance

There’s an integration in HACS which allows you to save the old value each time a sensor changes:

1 Like

Thanks Jack, I’ll try to figure that one out

An imperfect but easy and possibly still effective way (for my use case) might be to make a statistics helper sensor, averaging the source sensor’s output over, say, 48hours. So if the cloud sensor fails for a few hours or even a day, a relatively sane value would still be applied to turn my heaters on and off…

So now I need the sensor syntax - IF sensor A is unavailable, then sensor B

Any cleverer ideas still appreciated!

This would save the last valid value of the sensor:

alias: Test
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.yard_motion_sensor_light_level
    to: null
conditions:
  - condition: not
    conditions:
      - condition: state
        entity_id: sensor.yard_motion_sensor_light_level
        state: unavailable
      - condition: state
        entity_id: sensor.yard_motion_sensor_light_level
        state: unknown
actions:
  - action: input_number.set_value
    metadata: {}
    data:
      value: "{{ states('sensor.yard_motion_sensor_light_level') }}"
    target:
      entity_id: input_number.test
mode: single
3 Likes

Dangit jack too fast. :rofl:

Gotta use a trigger sensor so it retains state…

Jack’s sensor uses this methodology Weather - Home Assistant

English please… :grin:

1 Like

A more concise version:

alias: Test
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.yard_motion_sensor_light_level
    not_to:
      - unknown
      - unavailable
actions:
  - action: input_number.set_value
    metadata: {}
    data:
      value: "{{ states('sensor.yard_motion_sensor_light_level') }}"
    target:
      entity_id: input_number.test
mode: single
4 Likes

edits

I don’t know what you’re talking about (fat thumbs)

Another option is something like this:

template:
  - trigger:
      - platform: state
        entity_id: sensor.some_sensor
    sensor:
      - name: "Some new sensor"
        icon: ...
        unit_of_measurement: ...
        device_class: ...
        state: "{{ trigger.to_state.state if trigger.to_state.state | has_value else trigger.from_state.state }}"
4 Likes

There are more options, out of interest. I think my version is probably the most modern version, but I quite liked Petro’s version here too.

2 Likes

Thank you everyone. I’m going to go with the first one I can understand and make work :rofl:

2 Likes