The topic says it all: Is it possible to query the last time a light was ‘on’?
I want to create an automation that switches on a light but only if it wasn’t ‘on’ in the last three hours.
I currently created an automation that triggers when the light is switched off zo i can see the last time it was on by looking at the automation ast_triggered time but that seems a detour.
I’ve looked in the forum and Googled for info but i couldn’t find another way.
Is there any way of using light state information in automations?
I have a few automations that use the change of a switch or device_tracker state as a trigger to store the time it occured in an input_datetime.
That’s the easiest way I found so far.
Can’t see, why this shouldn’t work for light states as well.
As an example, this is what I use to record when the garden gate was last opened in an input_datetime:
# Info from ZWave Unit
- alias: Last Gate Opened Date and Time
trigger:
platform: state
entity_id: sensor.gardengate
from: 'off'
to: 'on'
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.gardengate_rev_last_opened
time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
The trick is getting it out of the database! I searched for “query history database” and most solutions end being either “no easy way to do it” or “use a python script”. That’s a shame.
Unfortunately, the ‘timestamp’ is what we’re looking for here, correct?
So, it’s hard to use it in the GET statement.
And as me - and many other people - have to delete or purge the database now & then I have taken even more ‘drastic measures’ to ensure that some of the values I rely on, e.g. precipitation for my automatic watering system, are stored completely outside the database.
For the precipitation info I store the values in a series of json files via a bash script:
#!/bin/bash
#RUN AS /bin/bash /home/homeassistant/.homeassistant/bash_files/save_precip_info.sh
# In bash
# Step 1 - delete precip_yday-9.json
rm -v /home/homeassistant/.homeassistant/files/precip_yday-9.json
# Step 2 - rename precip_yday-8.json to precip_yday-9.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-8.json /home/homeassistant/.homeassistant/files/precip_yday-9.json
# Step 3 - rename precip_yday-7.json to precip_yday-8.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-7.json /home/homeassistant/.homeassistant/files/precip_yday-8.json
# Step 4 - rename precip_yday-6.json to precip_yday-7.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-6.json /home/homeassistant/.homeassistant/files/precip_yday-7.json
# Step 5 - rename precip_yday-5.json to precip_yday-6.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-5.json /home/homeassistant/.homeassistant/files/precip_yday-6.json
# Step 6 - rename precip_yday-4.json to precip_yday-5.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-4.json /home/homeassistant/.homeassistant/files/precip_yday-5.json
# Step 7 - rename precip_yday-3.json to precip_yday-4.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-3.json /home/homeassistant/.homeassistant/files/precip_yday-4.json
# Step 8 - rename precip_yday-2.json to precip_yday-3.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-2.json /home/homeassistant/.homeassistant/files/precip_yday-3.json
# Step 9 - rename precip_yday-1.json to precip_yday-2.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday-1.json /home/homeassistant/.homeassistant/files/precip_yday-2.json
# Step 10 - rename precip_yday.json to precip_yday-1.json
mv -v /home/homeassistant/.homeassistant/files/precip_yday.json /home/homeassistant/.homeassistant/files/precip_yday-1.json
# Step 11 - Save Info from HAss
curl -k -H POST http://192.168.7.14:8123/api/states/sensor.pws_precip_today_metric > /home/homeassistant/.homeassistant/files/precip_yday.json
I agree, that might be extreme, but sometimes there is no one-size-fits-all solution.
I hope, though, that we’ve given the OP a few good ideas and they can choose what fits their need best.