Create a card to show how many times an entity is in a determinate state

Hello,
I would like to see in a card in realtime how many hours my pellet stove is in different state of functioning such as “Puissance 1”, “Puissance 2”, “Puissance 3” etc.

To achive this result I create 5 “input_datetime.puissance_*_start”, each one has this state “2023-01-24 00:00:00”.

Then I create 5 sensor into “sensors.yaml” to show the value in hours in a card in the frontend:

- platform: template
  sensors:
    puissance_1_hours:
      friendly_name: "Puissance 1 Hours"
      value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.input_datetime.puissance_1_start.state))|float/3600) }}"
    puissance_2_hours:
      friendly_name: "Puissance 2 Hours"
      value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.input_datetime.puissance_2_start.state))|float/3600) }}"
    puissance_3_hours:
      friendly_name: "Puissance 3 Hours"
      value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.input_datetime.puissance_3_start.state))|float/3600) }}"
    puissance_4_hours:
      friendly_name: "Puissance 4 Hours"
      value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.input_datetime.puissance_4_start.state))|float/3600) }}"
    puissance_5_hours:
      friendly_name: "Puissance 5 Hours"
      value_template: "{{ ((as_timestamp(now()) - as_timestamp(states.input_datetime.puissance_5_start.state))|float/3600) }}"

And at the end I create an automation to set the value of date and time of the input_datetime_puissance*start sensors based on the change of the state of pellet stove entity.

trigger:
  - platform: state
    entity_id:
      - sensor.info_pelletstove
condition: []
action:
  - if:
      - condition: state
        entity_id: info_pelletstove
        state: Puissance 1
    then:
      - service: input_datetime.set_datetime
        data: {}
        target:
          entity_id: input_datetime.puissance_1_start
  - if:
      - condition: state
        entity_id: info_pelletstove
        state: Puissance 2
    then:
      - service: input_datetime.set_datetime
        data: {}
        target:
          entity_id: input_datetime.puissance_2_start
  - if:
      - condition: state
        entity_id: info_pelletstove
        state: Puissance 3
    then:
      - service: input_datetime.set_datetime
        data: {}
        target:
          entity_id: input_datetime.puissance_3_start
  - if:
      - condition: state
        entity_id: info_pelletstove
        state: Puissance 4
    then:
      - service: input_datetime.set_datetime
        data: {}
        target:
          entity_id: input_datetime.puissance_4_start
  - if:
      - condition: state
        entity_id: info_pelletstove
        state: Puissance 5
    then:
      - service: input_datetime.set_datetime
        data: {}
        target:
          entity_id: input_datetime.puissance_5_start
mode: single

The problem is that it doesn’t work.

I have always strange values . For example the sensor.puissance_1_hours has the value of “17.1667600544294” which increases even if the pellet stove is off.

Another problem, for me, is how to create an automation that every day at midnight reset the value of the input_number_datetime and save the exiting value in another input_number variable.

Thank you in advance to everyone who will help me to figure out how to make it all work !!

Instead of all that, just use history stats sensors:

@tom_l Thank you so much ! I have complicated everything when instead it was possibile to reach the result only creating history stats sensors.
This is exactly what I was looking for :slight_smile:

1 Like