[SOLVED] Show hours:minutes, kWs and amount

Hi.

I control a switch that turns on/off a heating panel based on the ambient temperature. I know how many minutes it is on for a given date.

What I’d like to do is show that in a card probably. Something like this:

Heating Panel

  • Time: HH:mm
  • Watts: xx
  • Amount: $$

Where:

  • Time = minutes (from a template) to HH:mm
  • Watts = 500 every 60 minutes (panel consumption is 500W/h)
  • Amount = watts * cost per watt

TIA

Tried this template to convert minutes to hours without luck:

hours_panel_today:
  friendly_name: "Horas panel"
    value_template: >-
      {% set minutes = ( states('sensor.minutes_panel_today') | int * 60 ) %}
      {{ strptime(minutes, '%H:%M:%S').strftime('%H:%M') }}

I’m getting this in the logs:

TypeError: strptime() argument 1 must be str, not int

Watts is Power
kWh (or Wh) is Energy

So I don’t know what you are doing, you generally pay for “Energy”

The time conversion from decimal hours, needs to be coverted to seconds, not minutes
So 2 hours = 2 * 60 minutes = 2 * 60 * 60 seconds = 7200 seconds

{{ (120 * 60) | timestamp_custom('%R', false) }}

gives you 02:00

"{{ (states('sensor.minutes_panel_today') | int * 60) | timestamp_custom('%R', false) }}"

Sorry just re-read your post your sensor provides minutes so yes you were correct. The above has been corrected

For your unit cost per kWh ( say states(‘sensor.kwh_cost_per_unit’) )
cost would be : -

"{{ (states('sensor.minutes_panel_today') | int / 60) * (states('sensor.kwh_cost_per_unit') | float ) / 100 * 500 / 1000 }}"

The ‘500’ is your standard watts for the heater, I assume this is only whilst ‘on’ and the ‘minutes’ is the sum of minutes ‘on’ ???

I don’t know anyone who buys electricity in Wh’s, so Adjusted (as shown) for kWh’s

1 Like

Yeah, you’re right, kWh is the unit I’m aiming to calculate.

Correct.

Will try your suggestions and post back. Thanks!

Ok, ended up adding this to my configuration.yaml:

panel_hours_today:
  friendly_name: "Horas panel"
  value_template: >-
     {{ (states('sensor.panel_minutes_today') | int * 60) | timestamp_custom('%H:%M', false) }}

panel_cost_day:
  friendly_name: "Consumo"
  icon_template: mdi:currency-usd 
  value_template: >-
    {{ ((states('sensor.panel_minutes_today') | int / 60) * (states('input_number.costo_kwh') | float / 100) * (states('input_number.consumo_panel') | int / 10)) | round(2) }}

This way I can show the following info on the UI:

image

Was this meant for me ?
If you don’t reply to me then I don’t get a notification.
Luckily I just saw a familiar thread title.
Not sure what you pay per unit but that looks expensive
If on for 1 hr that heater consumes 1/2 kWh (what’s your unit cost?)
This shows 45 minues
Your templates are written differently to mine, no problem but why ?

Is this solved now ?

Kind off, but not necessarily. Just left the answer for the record, just in case anyone else is trying to do the same :slight_smile:

I live in Argentina, so 3.187 ARS = 0.047 USD. Not sure if that’s expensive.

My template looks pretty similar to yours:

Yours:

"{{ (states('sensor.panel_minutes_today') | int / 60) * (states('input_number.costo_kwh') | float ) / 100 * 500 / 1000 }}"

Mine:

{{ (states('sensor.panel_minutes_today') | int / 60) * (states('input_number.costo_kwh') | float ) / 100 * 500 / 10 | round(2) }}

I divide everything by 10 instead of 1000 to get the cost per time*kWh and round it to 2 decimals. I removed the variable part of the panel consumption though.

In your example, I would get 0.012216833333333335 instead of 1.22 (for a 0:46h period)

Okay, So you are using Pesos for unit cost AND final cost (Makes sense) as centevos are minimal.

Though if you are going for simplification of the formula you could just use

"{{ '%.2f' | format((states('sensor.panel_minutes_today') | int / 60) * (states('input_number.costo_kwh') | float ) / 2) }}"

I deliberately did not go for ‘simplification’, I went for maintainability and readability
So you have changed the wrong figures (it’s the same result but implicit meaning is lost)
the /100 was to convert cents to dollars (centevos to pesos, you are not doing this)
The 500 / 1000 was to convert power actually used in Watts to kilo Watts
So for you, the formula would be : -

{{ '%.2f' | format((states('sensor.panel_minutes_today') | int / 60) * (states('input_number.costo_kwh') | float ) * 500 / 1000) }}

For a ‘different’ heater (say) 700W, the formula would then be 700 / 1000 - easy to adapt/maintain.

Note the use of the text format statement that avoids the issue with epsilon errors AND forces the second decimal even if ‘0’

If this is solved please mark the post that helped the most as ‘solved’

1 Like

You rock! Thanks so much for the help!