Counter, that counts how many times a device has been turned on/off

Hey Guys,

I already made a post concerning this problem of mine but after I implemented the solutions the code still didn’t work. In a nutshell, I need a code, that says how often a device has been turned on. If it reaches more than 5.5 Watts, it is considered to be on and every time it goes below 5.5 it is considered to be off. Every time this cycle repeats, the counter has to be incremented by one but unfortunately it does not work. I use File Folder and have printed the code inside config.automations yaml, should it be located somewehere else? Should the paragraph with the binary sensor be inside another folder like sensors.yaml? My code might stil be wrong so I would really appreciate any sort of feedback, I genuienly am not sure where I could have potentially made a mistake.

counter:
  'counter.zahler_wasserspender':
    initial: 0
    step: 1
template:
  - binary_sensor:
      - entity_id: input_boolean.binary_sensor
        value_state: >
          {% if states('sensor.shellyplug_s_c8c9a3b8d590_power') | float(default=0) >= 5.5 %}
          to: 'on'
          {% elif states('sensor.shellyplug_s_c8c9a3b8d590_power') | float(default=0) < 5.5 %}
          to: 'off'

automation:
  alias: 'Increment if over measurement'
  trigger:
    platform: state
    entity_id: input_boolean.binary_sensor
    from: 'off'
    to: 'on'
  action:
    service: 'counter.increment'
    entity_id: 'counter.zahler_wasserspender'

Thanks for your time in advance,
Cheers

Have you looked at history stats?

I haven’t, how do you acces it?

In the documentation:
History Stats - Home Assistant (home-assistant.io)
But after a bit better reading, you would probably need the binary sensor to to trigger when it is below/above 5.5

1 Like
counter:
  zahler_wasserspender:
    initial: 0
    step: 1

template:
  - binary_sensor:
      - name: Zahler Wasserspender
        state: "{{ states('sensor.shellyplug_s_c8c9a3b8d590_power') | float(default=0) >= 5.5 }}"

automation:
  - alias: 'Increment if over measurement'
    trigger:
      - platform: state
        entity_id: binary_sensor.zahler_wasserspender
        from: 'off'
        to: 'on'
    action:
      - service: counter.increment
        target:
          entity_id: counter.zahler_wasserspender
1 Like

Am I right in saying that you are evaluating true / false as on and off ?

I didn’t realise you could do that.

That’s correct. From the documentation:

Thank you very much!

1 Like