Hello guys,
I recently started using HA and have amateur skills, meaning that I have difficulties getting along with the technicalities and YAML. Nevertheless, I tried my best to create a code, which allows me to see how many times a device has been turned on. This has been based on many templates and community posts I saw on the internet. The code is as follows:
counter:
'counter.zahler_wasserspender':
initial: 0
step: 1
-automation:
-alias: 'Increment if over measurement'
trigger:
platform: time_pattern
seconds: '/30'
condition:
condition: state
value_state: "{{ states('sensor.shellyplug_s_c8c9a3b8d590_power') | int >= 5.5 }}"
action:
service: 'counter.increment'
entity_id: 'counter.zahler_wasserspender'
If the device has a higher power than 5.5 Watts, it is considered to be on (based on the measurements and experiments I performed). I also created the counter with the ‘Helpers’ option, however for some reason the counter does not increment when the device is being used. I am not really sure if I have to use ‘state’ as a condition or ‘device’ and if ‘time_pattern’ is fitable as a trigger in that situation.
Where did I make any mistakes or do you have any suggestions?
Everything is extremely appreciated, thank you in advance!
So first of all please use the “Preformatted text (Crtl+E)” option to post any YAML code
That makes reading easier for everyone and as YAML is a formatted language a error typically is some space missing at the beginning.
In regards to your code (after I brought it to some useful formatting) I found several errors:
time_pattern triggers are always a bad idea …try to use them as “last change” only solution.
There are better options to really trigger your automation when it is needed only. - Check out: Automation Trigger - Home Assistant
your automation (if it would be working) would increment the counter EVERY 30 seconds as long as your shelly plug is higher than 5.5 watts… so e.g. if your shelly plus is 2 minutes higher than 5.5watts the count would be 4 - as I understood this is not what you want to achieve.
So, what is needed is a helper entity which keeps track if the power was lower than 5.5 watts since the last count. Only if the power goes lower than 5.5 watts once, it is considered off.
For point 3 there are two options how to solve it
a) MORE OBVIOUS - BEGINNER SOLUTION: Create an input_boolean which is turned on/off as part of the automation. Trigger the automation using numeric state trigger. One “turn_on” trigger if shelly plug is >= 5.5 v; One “turn_off” trigger if shelly plus is < 5.5.
Add for each trigger a condition that ensures
“on” tasks are only executed if the helper input_boolean is currently ‘off’ and
“off” tasks are only executed if hte helper input_boolean is currenly ‘on’
b) HA TEMPLATE SOLUTION - the one I will show here:
Create the counter entity:
counter:
zahler_wasserspender:
initial: 0
step: 1
Create a helper template binary_sensor. This binary sensor has a value of “on” if shelly plug is >= 5.5V and “off” if the shelly plug is < 5.5V.
automation:
- alias: "Automation Name"
trigger:
- platform: state
entity_id: binary_sensor.status_wasserspender
# adding from: off ensures not "unwanted" count if HA was offline and sensor state goes from "unknown" to "on"
from: "off"
to: "on"
action:
- service: counter.increment
target:
entity_id: counter.zahler_wasserspender
That picks up and loads all the files in that directory. This way all the config for an item is in one file. As you add more things this provides a good organizational structure to keep this stuff together. I have about 100 packages, The example I posted is the current detector package for a sump pump.
I track the number of times my pumps have cycled in the last hour, and I came here thinking I might offer some assistance. Instead I found your post which tracks even more. I will be using at least parts of this! Thanks!!
Hi! I have implemented this and it works awesome! I have a question though. Is there a user friendly way to view/run historical reports? For example, if I want to run a report for June 29, or even some date range or time range. All help is appreciated.
I’ve found that the HA database isn’t optimized for archival data or reporting like you describe.
I have a similar requirement for a sensor which totals up the run-time of my heating system boiler. I save daily run-time statistics to a flat file I can then analyze using external tools (like Excel or whatever statistical analysis package you use.