Using power consumption to show information (status, elapsed time, count) of cycles on a dish washer

Sorry, it’s indeed a binary sensor. Thanks for pointing that out, I’ve edited my reply.

Hi all! I’m trying to implement binary sensor code but while in developer tool → templates it returns True or False but the status of the binary sensor is always Unknown in state… This is my code:

binary_sensor:
  - platform: template
    sensors:
      washer_status:
        friendly_name: "Washing Machine"
        delay_on:
          minutes: 2
        delay_off:
          minutes: 6
        value_template: >-
          {{ states('sensor.shelly_shsw_pm_d8bfc019bffb_current_consumption') > 5 }}

With this response I have learned how custom sensors works, thanks so much!

1 Like

Just in case anyone had the same issue as me… i had to change the following:

{% set t = 0 if states('binary_sensor.washer_status') == 'off' ...

to:

{% set t = 0 if states('binary_sensor.washer_status.state') == 'off' ...

(to include state property of the binary sensor, otherwise it evaluates the whole object)

When I run {{ states(‘binary_sensor.washer_status.state’) }} in developer tools it outputs unknown, as for when I run {{ states(‘binary_sensor.washer_status’) }} it either returns on or off.

If it works for you then keep it, but it looks like a combination of two methods described in the Templating - Home Assistant (home-assistant.io) doc:

{{ states('device_tracker.paulus') }}
{{ states.device_tracker.paulus.state }}

hmm interesting… i wonder if it’s a difference with the integrations perhaps? For me the non-state version returns a whole object.

Just out of interest, did you mention you were counting the number of times per month/day that the washer was active? I’m keen to put that in place, but not sure what to use?

I’m not counting when an appliance is active, although you’ve planted a nice idea for the future😀 It could be something like a utility meter that resets weekly and is triggered by the time the appliance is turned on.

1 Like

Wow, genius! This is exactly what I try to accomplish, but I am totally stuck. Also I must admit I’m not skilled at all, in creating codes, scripts etc… I have the template sensor parts in my .YAML atleast.

I have a central wood boiler, which I would like to measure in some different ways for statistics and supervision. I want to know:

-If it’s burning, and for how long. If it have stopped, how long did it burn?
-How long has it been since the last fire burned out?
-Approximate amount of total wood consumed since last (manual) reset. One fire consumes about 130 litres of wood
-Total number number of fires since last (manual) reset.

I try to grasp your code and see if I could use it, and how to implement it, any help would be greatly appreciated!

All of the info I want to gather should be able to come from the smart plug I use to power it. It measures power consumption, and I know that if the boiler pulls 8-12W, it’s idle = no fire. When the internal fan starts, it pulls about 400W, then settles for around 200W until the wood is consumed and the fan switches off and the boiler pulls 8-12W again until next time I start a new fire.

Sometimes I start the fan while cleaning the boiler though, so ideally, the counters I want should not start until power have been > 100W for say 15 minutes.

Using your nice design in the UI, it should look something like this:

4c2a623c22ac734afa1a85db43eda7c456b19d0a

My sensor for tracking power usage is named sensor.power_127

I would be super grateful if anyone could help me get started with some code! :grinning:

I have atleast managed to create an automation for a counter that tracks how many fires that have been made. The code looks like this:

  alias: Vedpanna räknare
  description: ''
  trigger:
  - type: power
    platform: device
    device_id: 929394f7c6878c0d3509d5e89a6c241b
    entity_id: sensor.power_127
    domain: sensor
    above: 100
    below: 600
  condition: []
  action:
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.elpatron
  mode: single

In the UI it looks like this:

1 Like

Ok, the counter in the post above did NOT work :grinning_face_with_smiling_eyes:

I just lit a fire and it keeps increasing for some reason… I will have to try harder.

I still consider myself a noob, but hopefully this will get you in the right direction.
You could create the following binary sensor, it’ll change state to ‘on’ when it uses 100 watts and has being doing so for 15 minutes, in its ‘on’ state the icon is fire and in its ‘off’ state its fire-off:

- platform: template
  sensors:
    vedpanne_status:
      friendly_name: "Vedpanne status"
      delay_on:
        minutes: 15
      delay_off:
        minutes: 6
      value_template: >-
       {{ states('sensor.power_127') | float(0)> 100 }}
      icon_template: >
          {% if is_state('binary_sensor.vedpanne_status','on') %} 
          mdi:fire
          {% else %} 
          mdi:fire-off
          {% endif %}

The following sensor will show you how long the sensor is in the ‘on’ state:

- platform: template
  sensors:
    vedpanne_varaktighet:
      friendly_name: "Vedpanne varaktighet"
      value_template: >
        {% set x = states('sensor.time') %}
        {% set t = 0 if states('binary_sensor.vedpanne_status') == 'off' else now().timestamp() - states.binary_sensor.vedpanne_status.last_changed.timestamp() %}
        {{ t | timestamp_custom('%H:%M', false) }}

The time till last fire is an “inverted” vedpanne_varaktighet sensor, this will show the time since the last ‘off’ state of the vedpanne_status:

- platform: template
  sensors:
    vedpanne_varaktighet_tid:
      friendly_name: "Vedpanne varaktighet tid"
      value_template: >
        {% set x = states('sensor.time') %}
        {% set t = 0 if states('binary_sensor.vedpanne_status') == 'on' else now().timestamp() - states.binary_sensor.vedpanne_status.last_changed.timestamp() %}
        {{ t | timestamp_custom('%H:%M', false) }}

The following I haven’t tested, but it should display the number of fires over a 24-hour period:
(inspired by Count how many times a sensor crossed a threshold in a period - #3 by lujmoarf)

sensor:
  - platform: history_stats
    name: vedpanne_antal_brander
    entity_id: binary_sensor.vedpanne_status
    state: "on"
    type: counts
    end: "{{ now() }}"
    duration:
      hours: 24

For the consumed wood, there is a lot of possibilities, the following should give you the amount of time the burner was burning, you could use that number to calculate te amount of wood burned (I have created sensors that multiply values from one sensor with another one, but I have not yet multiplied time with a “fixed” value):

sensor:
  - platform: history_stats
    name: vedpanne_ved_brandes
    entity_id: binary_sensor.vedpanne_status
    state: "on"
    type: ratio
    end: "{{ now() }}"
    duration:
      hours: 24
2 Likes

Thank you very much for this! You have no idea how helpful this is! I will tinker with these codes and see what I can make with it!

You’re welcome, I used “float” while it’s a better practice to use “float(0)”, so I’ve changed that.

Really nice of you trying to help @hebom ! I try to use the codes you so kindly provided, but I can’t seem to make it work and I can’t find any new entities/sensors… I’m probably doing something really basic totally wrong, as I said, my skill level is super low… :upside_down_face:

Dumb question, but should these codes go in the configuration.yaml, the binary_sensor.yaml or the sensor.yaml?

I put it in the configuration.yaml (and the first code you wrote I put also in the binary_sensor.yaml) since one previous template sensor (“Effekt elmätare”) works fine and it’s configured there. The developer tools validate configuration says the config is valid. I have reloaded template entities and will also try a full restart of HA.

This is what my configuration.yaml looks like:


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate



group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


homeassistant:
   customize: !include customize.yaml

    
influxdb:
  host: (EDITED)
  port: (EDITED)
  database: Homeassistant
  username: Homeassistant
  password: (EDITED)
  max_retries: 3
  default_measurement: state
  tags_attributes:
    - friendly_name
    - hidden
  exclude:
    domains:
      - automation
      - group
      
sensor:
  - platform: airthings_wave
    scan_interval: 120
    elevation: 998
   
template:
  - sensor:
    - name: "Effekt elmätare"
      unit_of_measurement: "W"
      state: "{{ state_attr('sensor.consumption_56', 'power')| float / 1 }}"
      state_class: measurement
      device_class: power
      
    - name: "Gångtid elpatron"
      state: "{{ states('counter.elpatron') | float / 12 | round(1) }}"
      
  - platform: template
    sensors:
    vedpanne_status:
      friendly_name: "Vedpanne status"
      delay_on:
        minutes: 15
      delay_off:
        minutes: 6
      value_template: >-
       {{ states('sensor.power_127') | float(0)> 100 }}
      icon_template: >
          {% if is_state('binary_sensor.vedpanne_status','on') %} 
          mdi:fire
          {% else %} 
          mdi:fire-off
          {% endif %}
          
  - platform: template
    sensors:
    vedpanne_varaktighet:
      friendly_name: Vedpanne varaktighet
      value_template: >
        {% set x = states('sensor.time') %}
        {% set t = 0 if states('binary_sensor.vedpanne_status') == 'off' else now().timestamp() - states.binary_sensor.vedpanne_status.last_changed.timestamp() %}
        {{ t | timestamp_custom('%H:%M', false) }}
  
  - platform: template
    sensors:
    vedpanne_varaktighet:
      friendly_name: Vedpanne varaktighet
      value_template: >
        {% set x = states('sensor.time') %}
        {% set t = 0 if states('binary_sensor.vedpanne_status') == 'on' else now().timestamp() - states.binary_sensor.vedpanne_status.last_changed.timestamp() %}
        {{ t | timestamp_custom('%H:%M', false) }}
        
    sensor:
  - platform: history_stats
    name: vedpanne_antal_brander
    entity_id: binary_sensor.vedpanne_status
    state: "on"
    type: counts
    end: "{{ now() }}"
    duration:
      hours: 24        
        
    sensor:
  - platform: history_stats
    name: vedpanne_ved_brandes
    entity_id: binary_sensor.vedpanne_status
    state: "on"
    type: ratio
    end: "{{ now() }}"
    duration:
      hours: 24        
      
mqtt:
  discovery: false

Nah, not having very much luck:

* Invalid config for [template]: [delay_on] is an invalid option for [template]. Check: template->sensor->0->delay_on. (See /config/configuration.yaml, line 56).
* Invalid config for [template]: [value_template] is an invalid option for [template]. Check: template->sensor->0->value_template. (See /config/configuration.yaml, line 71).
* Invalid config for [template]: expected dictionary for dictionary value @ data['sensors']. Got [OrderedDict([('name', 'Vedpanne varaktighet2'), ('value_template', "{% set x = states('sensor.time') %} {% set t = 0 if states('binary_sensor.vedpanne_status') == 'on' else now().timestamp() - states.binary_sensor.vedpanne_status.last_changed.timestamp() %} {{ t | timestamp_custom('%H:%M', false) }} \n")])]. (See /config/configuration.yaml, line 78).
* Invalid config for [template]: [value_template] is an invalid option for [template]. Check: template->sensor->0->value_template. (See /config/configuration.yaml, line 78).
* Invalid config for [template]: [platform] is an invalid option for [template]. Check: template->platform. (See /config/configuration.yaml, line 86).

I’ve made some changes because friendly_name didn’t have quotes at one entity and on the other it did and there was a sensor that was double (the "interted verpanne veraktighet). The first three sensors should be binary sensors and the last two regular sensors. And after looking at: Template - Home Assistant (home-assistant.io) I’ve seen I could do some cleaning up with my configuration.yaml and other files :astonished: :sob:

After several frustrating nights trying to work out how the use templated sensors to track and display appliance use I stumbled across this discussion. Just a couple of hours later I have on/off status and run-time displayed for my washing machine (power sensing) and clothes dryer (vibration sensing), which ties in nicely with the alert automations I’m using.

Huge thanks to @pjotr1324 , @hebom , @Fete and everyone else for sharing your code and ideas!

1 Like

I’m running 2022.8.4 and modified this line to prevent template errors & warnings on a restart:

{% set t = 0 if is_state('binary_sensor.washer_status', 'off') else now().timestamp() - as_timestamp(states.binary_sensor.washer_status.last_changed, now().timestamp()) %}

1 Like

Glad you got it working! Would you mind sharing your code?

Sure thing. I’m using an energy monitoring GPO (Tapo P110) for the washing maching, and a vibration sensor (SW420) + Wemos D1 Mini clone set up in ESPHome for the dryer.

In my configuration.yaml…

binary_sensor:
  - platform: template
    sensors:
      washer_status:
      # Washing maching running (binary sensor test)
      # Modified from https://community.home-assistant.io/t/using-power-consumption-to-show-information-status-elapsed-time-count-of-cycles-on-a-dish-washer/382248/9
        friendly_name: "Washing Machine"
        delay_on:
          minutes: 2
        delay_off:
          minutes: 6
        value_template: >-
          {{ states('sensor.tapo_sp01_current_energy')|float(0) > 5 }} # If power consumption > 2.5 then running
        icon_template: >
          {% if is_state('binary_sensor.washer_status','on') %} 
          mdi:washing-machine
          {% else %} 
          mdi:washing-machine-off
          {% endif %}

  - platform: template
    sensors:
      dryer_status:
      # Clothes dryer running (binary sensor test)
      # Modified from https://community.home-assistant.io/t/using-power-consumption-to-show-information-status-elapsed-time-count-of-cycles-on-a-dish-washer/382248/9
        friendly_name: "Clothes dryer"
        delay_on:
          minutes: 2
        delay_off:
          minutes: 5
        value_template: >-
          {{ is_state('binary_sensor.wemos_d1_m03_dryer', 'on')}} # If vibrating > 1 min, assume ON
        icon_template: >
          {% if is_state('binary_sensor.dryer_status','on') %} 
          mdi:tumble-dryer
          {% else %} 
          mdi:tumble-dryer-off
          {% endif %}


- platform: template
  sensors:
    dryer_runtime:
    # Identify the current run-time of the appliance
      friendly_name: Clothes dryer runtime
      value_template: >
        {% set x = states('sensor.time') %}
        {% set t = 0 if is_state('binary_sensor.dryer_status', 'off') else now().timestamp() - as_timestamp(states.binary_sensor.dryer_status.last_changed, now().timestamp()) %}
        {{ t | timestamp_custom('%H:%M', false) }}

    washing_machine_runtime:
    # Identify the current run-time of the appliance
      friendly_name: Washing machine runtime
      value_template: >
        {% set x = states('sensor.time') %}
        {% set t = 0 if is_state('binary_sensor.washer_status', 'off') else now().timestamp() - as_timestamp(states.binary_sensor.washer_status.last_changed, now().timestamp()) %}
        {{ t | timestamp_custom('%H:%M', false) }}

And the esphome yaml code I added to the device config:

# Enables the SW-420 Vibration Sensor
binary_sensor:  
  - platform: gpio  
    name: $upper_devicename Dryer  
    pin:
      number: D2 # Don't use D3!
      mode: INPUT
    device_class: vibration
    filters:
     - delayed_on: 10ms # Debounce switch
     - delayed_off: 30s # Wait a couple of minutes after stop before triggering OFF

For anyone using an ESP8266 module in a sensor, pay attention to the GPIO pins you use as some prevent booting if pulled hi/lo.

I could probably trim the 2/6 and 2/5 lead/lag times down a bit, but this is working fine for me.

Next steps will be adding some conditions to the code to prevent “quiet time” notifications (thanks to suggestions from @Rod_Poplarchick), and replacing the ESP8266 D1 mini used for the dryer with an ESP-01 so it doesn’t feel like such a waste of resources :slight_smile: !

2 Likes