Can a restul sensor be prevented from updating?

I use restful sensors quite extensively to update other sensors, but on occasion those REST API calls fail and I then have dozens of other sensors all go to unavailable or error states.

It would be much more helpful if the sensor itself did not update in the event of a failure, and left the previous value unchanged.

This could be an attribute of the restful definition to only update the resulting sensor if the API call succeeds, but is there any way to do that today.

I can’t work out how to make all my other automations and sensors use the last known good value for the sensor in these situations.

Any ideas?

There are many ways to test for a valid value and keep the last known good value if not. Share one of your rest sensor configs.

just came back to deal with this…but I’m getting some very odd behaviour from the has_value filter…

{{ state_attr('sensor.solax', 'uploadTime') | has_value }}
{{ has_value(state_attr('sensor.solax', 'uploadTime')) }}
{{ state_attr('sensor.solax', 'uploadTime')  }}

returns me

False
False
2024-09-20 14:26:47

How can that be?

That’s because you’re using it wrongly.

has_value takes an entity ID and checks its state (ref).

You’re effectively checking for the state of an entity with an ID of '2024-09-20 14:26:27', which doesn’t exist for obvious reasons, so it returns False.

state_attr will return None if the attribute doesn’t exist.

OK, I get that…not sure why it is limited to only states, but ok.

However, I get this as well

{{ states('sensor.solax_local_battery_soc') | has_value }}
{{ states('sensor.solax_local_battery_soc') }}

resulting in

False
54

So I must be missing something obvious here now as that should surely be true?

Try this

{{ has_value('sensor.solax_local_battery_soc') }}

Thanks, I had just worked that out myself…trying to get my head around what “object” is being evaluated…and in the first example, the state isn’t the thing actually being evaluated it’s the value returned from the state and rendered…

I should know better coming from a java development background, but the documentation seems a bit misleading on this aspect of the language

As a filter, you’d want:

{{ 'sensor.solax_local_battery_soc'|has_value }}

It takes an entity ID and does the job of states() for you behind the scenes.