Show calculated template sensor as time

Hello friends,

I want to calculate the mean running time of my heating system.
Therfore I have etnitys for

  • running hours | float
  • impuses/start ups | int

to get the the mean I devide the running hours by the impulses.
I get a value somwhere like 3,0958
This value I want to translate into a time display as 03:05:26

I tried the following in the configuration.yaml:

template:
  - sensor:
      - name: "Laufzeit_WP"
        state: >
          {% set runtime = states('sensor.luxtronik_311202_026_compressor1_operation_hours') | float %}
          {% set impulse = states('sensor.luxtronik_311202_026_compressor1_impulses') | float %}
          {{ (states("runtime * 3600 / impulse") | float ) | timestamp_custom("%-H:%-M:%-S", false) }}

Do you guys have any suggestion?
Thanks

And it doesn’t work? If so, what’s the error?

This can’t work, as you won’t have “states” on that one. Try like this:

{{ runtime * 3600 / impulse | float | timestamp_custom("%-H:%-M:%-S", false) }}

EDIT: don’t think so, but I’m not sure if “runtime” might be a reserved word, so not usable for a variable name.

I don’t get an actual error. I just dont get a view on the dashboard
Where can I check my code for plausibility?

The timestep command expects a float in seconds, right?

Using your code did not change anything yet…can I force a update after editing the configuration.yaml?

For my learning: when do I use “states”?

Developer tools → Template

> template:
>   - sensor:
>       - name: "Laufzeit_WP"
>         state: >
>           {% set laufzeit = states('sensor.luxtronik_311202_026_compressor1_operation_hours') | float %}
>           {% set impulse = states('sensor.luxtronik_311202_026_compressor1_impulses') | float %}
>           {{ laufzeit * 3600 / impulse | float | timestamp_custom('%-H:%-M:%-S', false) }}

TypeError: unsupported operand type(s) for /: ‘float’ and ‘str’

What does that mean?

It means the template is attempting to perform an arithmetic operation where one of the values is a floating point number (float) but the other is a string (str) which is not a number and therefore invalid.

As an experiment, copy-paste the following template and let us know what it reports.

{% set laufzeit = states('sensor.luxtronik_311202_026_compressor1_operation_hours') %}
{{ laufzeit }}
{% set impulse = states('sensor.luxtronik_311202_026_compressor1_impulses') %}
{{ impulse }}

It should report two numbers.

It does report two numbert! :open_mouth:

But I get back string?!

I understand, I have to translate into binary

binary_sensor:
  - platform: template
    sensors:
        friendly_name: "Laufzeit VD1"
        value_template: "{{ states('sensor.luxtronik_311202_026_compressor1_operation_hours') | float(0) }}"

binary_sensor:
  - platform: template
    sensors:
        friendly_name: "Impulse VD1"
        value_template: "{{ states('sensor.luxtronik_311202_026_compressor1_impulses') | float(0) }}"

template:
  - sensor:
      - name: "Laufzeit_WP"
        state: >
          {% set runtime = states('Laufzeit VD1') | float %}
          {% set impulse = states('Impulse VD1') | float %}
          {{ (states("runtime * 3600 / impulse") | float ) | timestamp_custom("%-H:%-M:%-S", false) }

}

Now try this template in the Template Editor.


{% set laufzeit = states('sensor.luxtronik_311202_026_compressor1_operation_hours') | float(0) * 3600 %}
{{ laufzeit }}
{% set impulse = states('sensor.luxtronik_311202_026_compressor1_impulses') | float(0) %}
{{ impulse }}
---
{{ (laufzeit / impulse) | timestamp_custom('%-H:%-M:%-S', false) }}

It should report a value that looks something like this:
3:5:29

1 Like

Yeah that did the trick! Thank you!!!