I would like to see how many times in a day my sensor became on (from off). I can write automation to trigger for that sensor with state on but not sure how to keep count (local variable, database or something else) and increment it each time and also reset it to midnight as well the counter value on HA UI.
I did search the forum and found topic under feature request but I am ready to write automation/script to achieve it now until feature is done.
Worst case I see that I will have to write an application to keep track and push back via rest to HA and application will be called/triggered from the automation when state changes to on.
Thanks in advance.
when you have history on, you can always make a query on the DB.
you can find out the count for a day, for each day, the max on each day, the last, the first, etc.
if you use sqllitebrowser then its a piece off cake.
off course you can also use a pythonscript and even call it from HA.
i small script i made:
from requests import get
entitynaam='sensor.temperature_sensor_1_0'
datumwaarde='2016-06-26'
password='your_password'
url = 'http://192.168.178.20:8123/api/history/period/'+datumwaarde+'?filter_entity_id='+entitynaam
headers = {'x-ha-access': password,
'content-type': 'application/json'}
response = get(url, headers=headers)
lijstje=response.text.split('{"attributes":')
teller=0
for regel in lijstje:
#print(" ")
subregel=regel[:-2].split(",")
for onderdeel in subregel:
blankonderdeel2=onderdeel.replace("{","")
blankonderdeel=blankonderdeel2.strip("}")
gegeven1=blankonderdeel[0:blankonderdeel.find(":")]
gegeven2=blankonderdeel[blankonderdeel.find(":")+2:]
gegevennaam=gegeven1.replace('"','')
gegevenwaarde=gegeven2.replace('"','')
gegevenwaarde=gegevenwaarde.replace("+00:00","")
gegevenwaarde=gegevenwaarde.replace(datumwaarde+"T","")
if "changed" in gegevennaam:
print(gegevennaam + " = " + gegevenwaarde)
if "updated" in gegevennaam:
print(gegevennaam + " = " + gegevenwaarde)
if "state" in gegevennaam:
print(gegevennaam + " = " + gegevenwaarde)
if "state" in regel:
teller+=1
print("amount of entry's="+str(teller))
sorry that lots of parts are in dutch
Thanks for the help, I will see how easily I can achieve it and how I can display it on HA UI.
Thanks for the code/script.
I made python script to get me count but canât make it work under Home Assistant using shell command and I donât see sensor state get added/updated. It is not giving me error when I run manually script using command âpython /home/.homeassistant/scripts/find_ecobee_counter.pyâ
I am running Home Assistant on Ubuntu.
I have configuration like below:
shell_command:
run_ecobee_python_file: 'python /home/.homeassistant/scripts/find_ecobee_counter.py'
I have input_boolean.yaml to call this script
testing_toggle:
name: Testing Toggle
And automation.yaml is:
- alias: Testing Toggle
trigger:
- platform: state
entity_id: input_boolean.testing_toggle
action:
- service: shell_command.run_ecobee_python_file
- service: notify.gmail_xxxxx
data_template:
message: "Via Gmail - Testing: {{ now.strftime('%H:%M:%S.%f')[:-3] }}"
When I toggle the boolean I see the email coming out to my gmail but it is not doing what it needs to do under HA.
Below is the content of find_ecobee_counter.py
#!/user/bin/env python
# -*- coding: utf-8 -*-
import datetime
import json
from requests import get
from requests import post
base_url_get='http://localhost:8123/api/history/period/'
base_url_post='http://localhost:8123/api/states/sensor.downstairs_counter?api_password=xxx'
password='xxx'
requested_date=str(datetime.date.today())
entity_name='sensor.thermostat_downstairs_current_status'
headers = {'x-ha-access': password, 'content-type': 'application/json'}
cool_count=0
idle_count=0
fan_count=0
#print(requested_date)
url = base_url_get+requested_date+'?filter_entity_id='+entity_name
response = get(url, headers=headers)
data_str_list=response.text.split('{"attributes":')
for data_str in data_str_list:
if '"state": "cool"' in data_str:
cool_count+=1
if '"state": "idle"' in data_str:
idle_count+=1
if '"state": "fan"' in data_str:
fan_count+=1
#print("\tDownstairs\n\t\tcool=" + str(cool_count) + ", idle=" + str(idle_count) + ", fan=" + str(fan_count))
payload={"state":str(cool_count)}
response = post(base_url_post, data=json.dumps(payload))
#print(response)
Looks like I have figured out my issue I will update my post so it can help someone in future.
Issue was the location folder of the python script in my configuration. I had script hidden folder .homeassistant, now I moved it to higher up to itâs own folder and modified config from below:
shell_command:
run_ecobee_python_file: 'python /home/.homeassistant/scripts/find_ecobee_counter.py'
to like this:
shell_command:
run_ecobee_python_file: 'python ha_custom_scripts/find_ecobee_counter.py'
Then it started working. Still I donât know how to run HA in debug mode to see kind of errors to easily find out any issue.
Also I have noticed @fabaff updated my last post as it was messy without formatting, thanks for editing and fixing the formatting to make it pretty to read, now I have learnt how to post code using âPreformatted textâ or Ctrl+Shift+Q. Thanks a lot.
I know this is a fairly old thread⌠but I cannot get my HASS to execute anything now that Iâve moved to using a docker image on linux.
Ideally, i want a shell_command to run a python script. Watching the log, I see that it executes⌠but nothing happens. It doesnât actually execute. I tried everything I could think of, but no luck
As soon as I posted this, I got it to work. Although I donât fully understand it⌠I have to put the script into the path of the config. I then have to use the path /config/script.py in my shell_command. Iâm guessing that this is because Iâm using docker and the docker image only knows of the path /config? Anyway⌠it works now! Maybe this will help someone else.