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

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