Get the value of a sensor at a specific time

Hi

I would like to get the state of a sensor at a specific historical time. I can see it in the graph in the UI, but would like to have an automation based on this.
Example: what was the value of the sensor 3 hours ago?

I searched a lot but couldn’t find anything…

Thanks
Cadish

1 Like

This post here will help.

Thanks a lot @petro! Will try this…

Anyway, it’s strange that to have it in an easier way is not a wanted feature for a lot of people.

1 Like

Oddly enough, not many people want this. I think I’ve seen ~4 posts over the past 4 years.

1 Like

Does seem like a odd use case. What automation would you want triggered specifically at a 3hr past moment in time?otherwsie historical values can be seen in the log charts, or grafana.

My use case: I have a Zigbee light that defaults to ON after every power failure. Now I want to create an Automation to switch off the light after the power failure was resolved. BUT, I only want it to happen if the light was OFF before the power failure occurred. Thus, I want to query the light’s state at the time just before the power failure occurred and if it was off, then the light must be switched off by the automation.

Hi,

do you get a solution. I want to switch back my devices after a power failure to the old state but I get it not running.

Thanks, Steffen

You can use an “input select” for this, without the query:
Set the value, “on”, “off”, “dimmed” or whatever, based on events and create an automation that triggers on change of the “input select” and sets e.g. scenes.
When the power is back on again execute the automation and you’re good.
Also handy when you want to activate another scene temporarily, e.g. as action on a motion event and go back to the previous state after.

1 Like

Thought i would add my use case for being able to retrieve a historic sensor value: i have a humidty sensor in my shower. When the humidity spikes at a certain rate, i assume the shower is being used, and switch the air ventilation system to a higher value. I would like to be able to retrieve the value of the humidity sensor just before the spike starts, so that when the value drops to (close to that) pre-spike-value, i switch the air ventilation back to a lower gear. I now have it run 30 minutes at the high capacity, but maybe often times that is too long, wasting energy, or maybe sometimes it is to short.

I will see whether i can solve it with SQL - Home Assistant and Sensor state 24 h ago - #13 by alfwro13

Make it 5 now. I’m seeing an errant value being displayed in a historical graph and the manufacturer wants ME to dig up the discrete value to prove it’s their problem, not an HA problem.

Another use case would be to notify me if my propane tank got filled.

If sensor.propane(-15min) - sensor.propane(-2hr) > 25 then send notification

I don’t want an alert to be sent when my propane goes from 25% to 50%, I want an alert sent once my propane guy is done filling the tank (which he will fill to 80%), hence historical values would be valuable.

You can use the derivative integration to do that.

I’d like to request that feature too.

Hello all!

Here is my use case. I use a Shelly EM to monitor power usage in my home. When I run the dishwasher I see a distinct pattern in power usage. It looks like this:

Imgur

My plan is to be able to reference the old data points near specific time intervals (like comparing the values 15, 17, 19 minutes ago with a threshold of 2000), and other values with a threshold of lower than 2000, and declare that the dishwasher cycle is complete and fire a notification. (basically validate that the pattern has happened).

I see that a possible solution would be to use an SQL sensor (Sensor state 24 h ago - #2 by MaxK), but that means creating a bunch of extra sensors, that in turn create noise in the database, and it’s less than ideal.

Thanks

That’s the only way to do it currently.

You did read about this way of accomplishing what i think you want: Get notified when your Washing Machine has finished it's wash cycle using Smart Switches and Contact Sensors — Home Automation Guy ? I have been running it like that for a couple of years now

@Troon thank you for that link! I’m amazed that works :smiley:
I’ve implemented it into a template binary sensor that looks like this:

{% if state_attr('sensor.power_meter_channel_0_history', "min14") > 2000 and state_attr('sensor.power_meter_channel_0_history', "min16") < 2000  and state_attr('sensor.power_meter_channel_0_history', "min18") > 2000 and state_attr('sensor.power_meter_channel_0_history', "min20") < 2000 and state_attr('sensor.power_meter_channel_0_history', "min22") > 2000  and state_attr('sensor.power_meter_channel_0_history', "min30") < 2000 %}
On
{% else %}
Off
{%endif%}

Now I’ll have to run the dishwasher and see if I got it right, and fire a notification based off it.

@raoul.teeuwen I know about that approach, but it seems wasteful to use a dedicated power sensor when my appliance has a particular power usage pattern. Since power spikes to > 2KW (and other things around the house don’t - including 2 ACs), I thought I could look for that pattern without additional hardware. Also, I’m not interested in following the appliance cycle (idle/running/finished), I just want to know when it’s finished.

I’ll take that as a compliment :rofl:

Just for neatness, a binary sensor template needn’t return On or Off: just a logical true / false will do. This does the same job:

{{ state_attr('sensor.power_meter_channel_0_history', "min14") > 2000 and
   state_attr('sensor.power_meter_channel_0_history', "min16") < 2000 and
   state_attr('sensor.power_meter_channel_0_history', "min18") > 2000 and
   state_attr('sensor.power_meter_channel_0_history', "min20") < 2000 and
   state_attr('sensor.power_meter_channel_0_history', "min22") > 2000 and
   state_attr('sensor.power_meter_channel_0_history', "min30") < 2000 }}