Hello,
after spending (and failing) the whole afternoon, I’m here to ask for your help.
I want to display in dashboard 2 values:
- How long a device was on
- The average time over a time window about the how long a device was on
After a bit of experiments, I found the first part to be working
A smart plug with an energy meter is used on a dishwasher.
Here I define a sensor to determine when the device is on (power consumption > 3.0 watts)
binary_sensor:
- platform: template
sensors:
lavastoviglie_operative_state:
friendly_name: "Stato Accensione Lavastoviglie"
value_template: "{{ states('sensor.lavastoviglie_current_power') | float > 3.0 }}"
device_class: power
Then i defined a timer that starts when the binary sensor switch to state “on”
(thanks Time elapsed counter - #2 by pnbruckner and How long switch has been on for? - #2 by 123 for this)
package_lavastoviglie_timer:
input_datetime:
lavastoviglie_last_on:
has_date: true
has_time: true
automation:
- trigger:
platform: state
entity_id: binary_sensor.lavastoviglie_operative_state
to: "on"
action:
service: input_datetime.set_datetime
entity_id: input_datetime.lavastoviglie_last_on
data_template:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
- trigger:
platform: time_pattern
seconds: "*"
condition:
condition: state
entity_id: binary_sensor.lavastoviglie_operative_state
state: "on"
action:
service: homeassistant.update_entity
entity_id: sensor.lavastoviglie_on_time
sensor:
- platform: template
sensors:
lavastoviglie_on_time:
value_template: >
{{
(as_timestamp(now()) - state_attr('input_datetime.lavastoviglie_last_on', 'timestamp')) |timestamp_custom('%H:%M:%S', false)
if states('binary_sensor.lavastoviglie_operative_state') == 'on'
else None
}}
recorder:
exclude:
entities:
- input_datetime.lavastoviglie_last_on
- sensor.lavastoviglie_on_time
And this part is working, few seconds after I power the dishwasher, the timer starts and it stops after the dishwasher ends the washing cycle and power off itself.
Now, moving to the second part. While the first part is not actually storing “how long the dishwasher was on”, it is only a timer, I would like to have a way to display the average duration of when the dishwasher is on.
Basically I’m looking about an average time for the washing cycle.
So that when I compare the timer of current cycle with the average on time, I can roughly estimate how long until the cycle is over.
I tried the platform: statistics
, I tried the custom average sensor (Average Sensor), I tried the query method, nothing worked.
Can you help me on this?
Thanks