Hour Meter Counter

Hi all, I’m trying to think through a way to get an odometer style meter for lifetime usage of a UV light bulb.

Most of the suggestions involve changing HA to store data longer for states, but the UV bulb is expected to last 14 months of runtime and then needs replaced. I know I can use history-stats to get the ON runtime of the UV light from my switch sensor, but what is the best way to take this value and store it or add it to a running total sensor/counter to have lifetime value?

I was thinking, I could have an automation at 11:59 run and update the hours total on another sensor. Should this be an input_number or can I use a counter? Should I just setup an automation to trigger a counter to step increment when its been on for 1 hour and somehow loop it?

Example value at the end of a day.

value: 7h 28m
unit_of_measurement: h
friendly_name: Red Sea Aqua UV Runtime Today
icon: 'mdi:chart-line'

Thanks!

1 Like

This is what i use

sensor:
  - platform: history_stats
    name: UV_Hour_Meter
    entity_id: switch.uv
    state: 'on'
    type: time
    start: '{{ 0 }}'
    end: '{{ now() }}'    

recorder:
  purge_keep_days: 365
  commit_interval: 60 
  include:
    entities:
      - sensor.uv_hour_meter

Only problem is your entities will stop logging but you can added them to the record and they will for 365 days, it hasn’t been a problem for me

1 Like

I have a similar situation where I want to count the total amount of runtime hours a vacuum tube has been energized.
I used your method and it seems to kind of work, but the total amount of hours is ridiculous: 477.660:07 which calculates to 54 years or so :rofl:

Here’s my configuration:

sensor:
  - platform: history_stats
    name: buis
    entity_id: switch.eindversterker_schakelaar
    state: "on"
    type: time
    start: "{{ 0 }}"
    end: "{{ now() }}"

recorder:
    purge_keep_days: 365
    commit_ interval: 60
    include:
       entities:
          - sensor.buis

I have exactly the same thing going on (tracking usage of a UV-bulb for water sterilization that needs to be replaced after 365 days of use), but even though you all have made great examples of how to do this I am stuck. I’m a total noob when it comes to programming, so please bare with me. I added this to the end my configuration.yaml, since that is apparently where these kinda things go (?) :

sensor:
  - platform: history_stats
    name: UV_Lamp_hours
    entity_id: switch.shellypro4pm_08f9e0e9ed64_switch_0
    state: 'on'
    type: time
    start: '{{ 0 }}'
    end: '{{ now() }}'    

recorder:
  purge_keep_days: 365
  commit_interval: 60 
  include:
    entities:
      - sensor.uv_lamp_hours

Then I reloaded “ALL YAML CONFIGURATIONS” and expected to be able to add a new entity called “uv_lamp_hours” to my dashboards. But no luck.

What am I doing wrong?

I ended up doing mine a little differently. I have a state I save as an input_number and then use an automation to increment it based on the amount of usage in the given day.

I dont have a Shelly so I am not show the format of their numbers but my sensor I had to setup formatting to get the times. My whole config is on github in case it helps.

Interesting, thank you for sharing.

On first sight I unfortunately believe this to be over my pay-grad in terms of coding. But I’ll try and see where it gets me!

@santaklon , which solution did you land on? I have a UV light I use for my swimming pool. The light comes on together with the filter/circulation pump and the running hours each day varies with the actual pool temperature. Therefore it would be good to monitor actual running hours and how many hours are left on the UV light bulb :slight_smile:

Until now unfortunately no solution.

In my case the UV-Lamp (together with the chlorinator) are on 24/7 when the house is inhabited, so its somewhat easy to keep track of the usage for now. But I would still like a solution for this. The provided ideas might work, but I’m really not a programmer and while I tried copy-pasting these lines into my config.yaml, I dont understand the code enought to adapt it to my installation / make it work.

I tried building something with NodeRed, but that seems unsuitable because it restarts counters whenever its is deployed.

Btw.: I was told the warm-up of the lamp when switched on takes quite a toll on the lifespan. So on one hand one should not switch it on/of too often but rather just keep it on and also I’m wondering if this would have to be somehow integrated in the counter…

Here’s how I count the UV usage on my lamp. I have it on a smart switch and it does turn off when we are away for more than a day.

Package

homeassistant:
  customize:
    sensor.um_water_uv_uv_usage:
      unit_of_measurement: hours

input_number:
  um_water_uv_filter_life:
    min: 0
    max: 50000
    step: 1
    unit_of_measurement: hours
    mode: box

input_button:
   um_water_uv_filter_reset:
     name: um_water_uv_filter_reset


template:
  - trigger:
      - platform: state
        entity_id: input_button.um_water_uv_filter_reset
    sensor:
      - name: "um_water_uv_filter_replaced"
        state: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }} "
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      - name: um_water_uv_per_hour
        unit_of_measurement: hours
        state: >-
          {{ iif(is_state("switch.water_uv","on"),1,0) }}
        attributes:
          triggered_at: "{{ now().hour }}"
  - sensor: 
      - name: um_water_uv_filter_life
        unit_of_measurement: "%"
        state: "{{ (((1.0 - ( states('sensor.um_water_uv_uv_running')|float(0) / states('input_number.um_water_uv_filter_life')|float(0) )))*100.0)|round(1) }}"

sensor:
  - platform: integration
    source: sensor.um_water_uv_per_hour
    name: um_water_uv_uv_usage
    unit_time: h
    method: left
    round: 2

utility_meter:
  um_water_uv_uv_running:
    source: sensor.um_water_uv_uv_usage

recorder:
  include:
    entities:
      - sensor.um_water_uv_uv_running
      - sensor.um_water_uv_uv_usage
      - sensor.um_water_uv_per_hour
      - input_number.um_water_uv_filter_life
      - input_button.um_water_uv_filter_reset
      - sensor.um_water_uv_filter_life
      - sensor.um_water_uv_filter_replaced

Automation

- id: reset um_hvac_uv filter
  alias: reset um_hvac_uv filter
  description: ""
  mode: single
  trigger:
    - platform: state
      entity_id: input_button.um_hvac_uv_filter_reset
  action:
    - service: utility_meter.calibrate
      data:
        value: "0"
      target:
        entity_id: sensor.um_hvac_uv_uv_running
        

This is EXACTLY what I’m loking for!

Unfortunately I am way too mauch of a noob to understand what all this does. The most I can do is change the entity names to match my configuration.

So please bear with me here - where do I put the “package” you posted? Does that go into the config.yaml? Or do the sections go into different places?

The Automation i can just copy-paste into an empty automation, I guess.

You could put it in configuration.yaml or put it in a package file. To do the later, put this into configuration.yaml

homeassistant:
  packages: !include_dir_named packages

Create a directory called packages and then create a new yaml file and paste the contents in

Hello again!

Please bear with me once again, as I am a complete YAML / coding-noob and anything beyond click-pointy-UI is beyond my brain capacity.

So I have tried recreating your set-up. Not for the UV-Lamp (since its maintenance-cycle is a bit long for testing) but first for a pellet-burner that needs cleaning about every 30 hours of burn-time.

Some of the enities do show up as expected (but the equivalent of your “source” and um_water_uv_uv_running" does not) but no change can be seen when the burner is on.

image

image

Admittedly even after studying your code I have no clue what it all is doing since I am no coder myself. I just have just changed the names and entities.

Package:

(The entity sensor.pellet_heating_status is a templatet sensor i created from the power readings of the burner that does reliably report “burning” when it is in fact burning)

homeassistant:
  customize:
    sensor.heatingmaintenance:
      unit_of_measurement: hours

input_number:
  burntime_remaining:
    min: 0
    max: 30
    step: 1
    unit_of_measurement: hours
    mode: box

input_button:
   burntime_reset:
     name: burntime_reset


template:
  - trigger:
      - platform: state
        entity_id: input_button.burntime_reset
    sensor:
      - name: "heating_last_cleaned"
        state: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }} "
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      - name: burntime_per_hour
        unit_of_measurement: hours
        state: >-
          {{ iif(is_state("sensor.pellet_heating_status","burning"),1,0) }}
        attributes:
          triggered_at: "{{ now().hour }}"
  - sensor: 
      - name: burntime_remaining
        unit_of_measurement: "%"
        state: "{{ (((1.0 - ( states('sensor.heating_burning')|float(0) / states('input_number.burntime_remaining')|float(0) )))*100.0)|round(1) }}"

sensor:
  - platform: integration
    source: sensor.burntime_per_hour
    name: burntime
    unit_time: h
    method: left
    round: 2

utility_meter:
  heating_burning:
    source: sensor.burntime

recorder:
  include:
    entities:
      - sensor.heating_burning
      - sensor.burntime
      - sensor.burntime_per_hour
      - input_number.burntime_remaining
      - input_button.um_water_uv_filter_reset
      - sensor.burntime_remaining
      - sensor.heating_last_cleaned

automation:

  alias: clean pellet heating
  description: ""
  mode: single
  trigger:
    - platform: state
      entity_id: input_button.burntime_reset
  action:
    - service: utility_meter.calibrate
      data:
        value: "0"
      target:
        entity_id: sensor.heating_burntime

After adding the Automation HA threw an error message, that it doesn’t know “utility_meter”. Figured out thats an integration and installed it. I tried to create a matching utility meter but have no clue how to configure that part either, since it seems for tracking e.g. energy, but not time.

Also did not quite figure out what your sensors actually represent / what the numbers would tell me and how to translate them to my example:

image

Any help is greatly appreciated.
Thank you in advance!