Log sensor uptime if value is > X

Hi, I would need some help in deriving boiler costs.
I have a shelly 1 pm connected to the wires of the boiler that gives me the value in W of the consumption.
I know that when the boiler is on it consumes 62/64 W.
How can I create an entity that gives me the uptime of the boiler only when it is on?
That is, I would like a sensor that would log only when the 60W is exceeded and calculate the uptime.

sensor.consumi_caldaia_power

Thanks

create a binary template sensor that determines that it will be on when sensor.consumi_caldaia_power’s is above 60. Then create a history stats sensor, tabulating the total time.

use this template in a template binary_sensor

{{ states('sensor.consumi_caldaia_power') | float(0) > 60 }}

And then make a history stats sensor with type: time

1 Like

Really thank you

If you end up with a working solution, please post it and mark that as the solution for others.

already done

I was referring to any yaml you added to your configuration. Thank you though.

Ah ok sorry.

I’ve created a binary_sensor if consumption is > 60w

binary_sensor:
  - platform: template
    sensors:
      consumi_caldaia_shel:
        friendly_name: Consumi caldaia Shelly
        entity_id:
          - sensor.consumi_caldaia_power
        value_template: >-
          {{ states('sensor.consumi_caldaia_power') | float(0) > 60 }}

After i’ve create a history_stats for 24 log uptime (from 00:00 to 23:59)

  - platform: history_stats
    name: Uptime Caldaia shel
    entity_id: binary_sensor.consumi_caldaia_shel
    state: "on"
    type: time
    start: "{{ today_at('00:00') }}"
    end: "{{ today_at('23:59') }}"

And after this i’ve create a gas meter to calculate real consumption cost

template:
  - sensor:
      - unique_id: my_gas_template_sensor
        name: Gas Meter
        state_class: total_increasing
        device_class: gas
        unit_of_measurement: m³
        state: "{{ (states('sensor.uptime_caldaia_shel') | float(0) * 0.9)  | round(1) }}"
        availability: "{{ states('sensor.uptime_caldaia_shel') | is_number }}"