How do I set a retained variable down to 1 decimal place

Hi I have Shelly Temperature sensors (H&T) and there is a problem that if they go off line you don’t know for many hours. I use these for my central heating, so that’s a problem.

So I want to run a script every hour.
I want to compare the current temperature with the one taken an hour ago (a variable)
If the temperature has changed then update the variable
If the temperature is the same then I can set an input Boolean

The temperature sensor is 1 decimal place, so what can I use for my variable?

Regards, Dave

Why not just look at the last_changed property of your sensor?

Did not know that existed, good call but just tried to print the value using Development Tools > Templates

{{states ('sensor.shelly_temp_lounge_temperature.last_changed')}}

and get result “Unknown”

It is a property of the entity (not an attribute or state). You can not use the states() method to access properties of entities. You access it like this:

{{ states.sensor.shelly_temp_lounge_temperature.last_changed }}

Note this datetime will only update if the value of the state changes. So repeated readings of the same value will not update it.

Thanks, have written thousands of lines of Basic, Assembler & C over the years but really struggle with templates.

The result is now 2023-10-16 13:06:43.169159+00:00, but the temperature has changes a number of times since then.

So using this method, will still have to somehow compare this time to the current time and come up with a value.

That time is in UTC. Try:

{{ states.sensor.shelly_temp_lounge_temperature.last_changed|as_local }}

Thanks for your time, that just gives me +1 at the end

2023-10-16 14:06:43.169159+01:00

That’s the timezone offset for your location.

The time has also changed to 14:06. Which if you look at the graph is when your sensor last changed.

Had not noticed that.

Can you trigger an automation on just a change of temperature? i.e. without putting a value in?

Yes.

trigger:
  - platform: state
    entity_id: sensor.temperature
    to: 

The null to: triggers on all state changes but ignores attribute changes. Leave it out and it will trigger on attribute changes too.

To trigger on any state being the same value for an hour:

trigger:
  - platform: state
    entity_id: sensor.temperature
    to: 
    for:
      hours: 1

Many thanks, this could be a great solution, will give it a try.

Much appreciate your time.

Kind regards, Dave

1 Like

Thinking about it, that may trigger after a restart (all states go unknown on restarting). So this would be better:

trigger:
  - platform: state
    entity_id: sensor.temperature
    not_from:
      - unknown
      - unavailable
    for:
      hours: 1

Ok, I understand that it will trigger in a change of temperature or humidity, I have tested that and it works.

Don’t get the logic of the timer, if it does not trigger because its gone off line how does this ever trigger?

What I intended to do is reset a timef every time the temperature changes.

If it changes to any state (including unknown or unavailable) and stays that way for an hour it will trigger.

That’s the problem I started with, when they go off line it takes up to 24 hours before the become unavailable.

Scenario: heating comes on set to 20 deg, temp sensor went off line at 18 deg, HA just see’s 18 deg, so the heating will never go off. (I have had this happen to me)

So I will set a timer when the heating comes on, and reset the timer via this automation, (so its constantly being reset as the temperature should be changing) if the timer reaches an hour I can get it to turn the heating off. (worse case the heating runs for 1 hour)

Yeah but as soon as the device goes offline the state will not update and will stick with the same value for over an hour triggering the automation.

Tried it (unplugged the Shelly) and it did not trigger?

After an hour?

yes, set timer for 30min

platform: state
entity_id:
  - sensor.shelly_temp_lounge_temperature
for:
  hours: 0
  minutes: 30
  seconds: 0