Output the total on-time of a sensor daily

Hey Werner,

sure, here are a few examples.
Personally, I find the modern config style to be easier once you get to know it a little better :smiley:

Legacy

- platform: template
    sensors:
      arbeitszimmer_luftung_gesamt_minuten:
        friendly_name: "Arbeitszimmer Gesamt Lüftung Minuten"
        unit_of_measurement: "Min"
        value_template:  "{{states('sensor.arbeitszimmer_luftung_gesamt')|float * 60 }}"

Modern

template:
  - sensor:
      - name: "Arbeitszimmer Gesamt Lüftung Minuten"
        unit_of_measurement: "Min"
        state: "{{states('sensor.arbeitszimmer_luftung_gesamt')|float * 60 }}"
# To add more sensors, simply add them here like this one:
      - name: "Another sensor"
        state: "OK"
# To add binary sensors do this:
  - binary_sensor:
       - name: "Example Binary Sensor"
         state: "{% if true %}on{% endif %}"

Alternatively, to keep configuration.yaml clean:

# In configuration.yaml
template: !include template.yaml
# In template.yaml (in the same directory as configuration.yaml)
# Note: Do NOT add "template:" here,
# since you've already done that in configuration.yaml
- sensor:
    - name: "Arbeitszimmer Gesamt Lüftung Minuten"
      unit_of_measurement: "Min"
      state: "{{states('sensor.arbeitszimmer_luftung_gesamt')|float * 60 }}"
# To add more sensors, simply add them here like this one:
    - name: "Another sensor"
      state: "OK"
# To add binary sensors do this:
- binary_sensor:
     - name: "Example Binary Sensor"
       state: "{% if true %}on{% endif %}"

Difference - for most sensors:

  • friendly_name: changes to name:
  • value_template: changes to state:

These are the valid modern configuration variables, these are the legacy ones.

Your “Hoftorschalter” (which is a switch) won’t need to be migrated, the template integration only supports sensors, binary (on/off) sensors, buttons, numbers and selects. See the second paragraph on the docs page

Everything else will (for now?) stay in their own domain, just like your switch.
So if you have any template (binary) sensors, buttons, numbers, selects, those will have to be migrated (at some point).

1 Like

Hello,

many thanks for the support. (German below). Will try to implement it.

Am I correct in saying that sensors always have to come under one another?
So far I haven’t sorted it that way.

Here is an excerpt of my config to clarify:

# Datum und Uhrzeit    
sensor:
  - platform: template
    sensors:
      datum_zeit_time:
        friendly_name: ""
        value_template: >
          {% set days = ['Mo.', 'Di.', 'Mi.', 'Do.', 'Endl.Fr.', 'Sa.', 'So.'] %}
          {{ days[now().weekday()] }}
          {{ now().strftime('%d.%m. - %H:%M') }}  Uhr

          
# Fensteröffnungen Gesamtdauer

  - platform: history_stats
    name: Arbeitszimmer Lüftung Gesamt
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_19330507_on_off
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

  - platform: template
    sensors:
      arbeitszimmer_luftung_gesamt_minuten:
        friendly_name: "Arbeitszimmer Gesamt Lüftung Minuten"
        unit_of_measurement: "Min"
        value_template:  "{{ (states('sensor.arbeitszimmer_luftung_gesamt')|float * 60) | round }}"


  - platform: history_stats
    name: Moritz Lüftung Gesamt
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_b460d606_on_off
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    
  - platform: template
    sensors:
      moritz_luftung_gesamt_minuten:
        friendly_name: "Moritz Gesamt Lüftung Minuten"
        unit_of_measurement: "Min"
        value_template:  "{{ (states('sensor.moritz_luftung_gesamt')|float * 60) | round }}"
        

# Systemmonitor
  - platform: systemmonitor
    resources:
      - type: processor_use
      - type: processor_temperature
      - type: load_1m       
      - type: load_5m       
      - type: load_15m  
      - type: memory_free
      - type: memory_use
      - type: memory_use_percent
      - type: disk_use
        arg: /
      - type: disk_free
        arg: /
      - type: disk_use_percent
        arg: /
      - type: network_in
        arg: eth0
      - type: network_out
        arg: eth0
      - type: last_boot

# PV-Anlage Sensoren aus "sma power"
  - platform: template
    sensors:
      pv_ertrag_aktuell:
        friendly_name: "PV-Ertrag Aktuell"
        unit_of_measurement: "Watt"
        value_template:  "{{ state_attr('sensor.sma_power', 'PV') }}"
        icon_template: mdi:solar-power
  - platform: template
    sensors:
      pv_einspeisung_aktuell:
        friendly_name: "PV-Einspeisung Aktuell"
        unit_of_measurement: "Watt"
        value_template:  "{{ state_attr('sensor.sma_power', 'FeedIn') }}"
        icon_template: mdi:transmission-tower-import

# Fensterstatus  
      fensterstatus_schlafzimmer_dg:
        friendly_name: Fenster Wohnzimmer-DG
        value_template: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} geschlossen
          {% elif b1 == 'on' and b2 == 'off' %} gekippt
          {% elif b2 == 'on' %} offen
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} nicht verfügbar
          {% else %} ?
          {% endif %}
        icon_template: |
          {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
          {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_on_off') %}
          {% if b1 == 'off' and b2 == 'off' %} mdi:window-closed-variant
          {% elif b1 == 'on' and b2 == 'off' %} mdi:angle-acute
          {% elif b2 == 'on' %} mdi:window-open-variant
          {% elif b1 == 'unavailable' or b2 == 'unavailable' %} mdi:exclamation
          {% else %} ?
          {% endif %}

So then I can no longer freely switch between e.g. - platform: template, - platform: system monitor, - platform: history_stats?

Greetings Werner

Hallo,

vielen Dank für die Unterstützung. (Deutsch unten). Werde es versuchen umzusetzen.

Sehe ich es richtig, dass Sensoren immer untereinander kommen müssen?
Bisher habe ich dies nicht so sortiert.

Hier mal ein Auszug meiner config, um dies zu verdeutlichen:

siehe oben…

Ich kann also dann nicht mehr zwischen z.B. - platform: template, - platform: systemmonitor, - platform: history_stats frei wechseln?

Liebe Grüße Werner

Hello Werner,

depending on your configuration, you might want to split up configuration.yaml into multiple files.
There are a few ways to do that, please read through the docs on splitting up the config.
Be aware of this:

Some integrations support multiple top-level !includes, this includes integrations defining an IoT domain, e.g. light, switch, sensor as well as the automation, script and template integrations, if you give a different label to each one. Configuration for other integrations can instead be split up by using packages. To learn more about packages, see the Packages page.

1 Like

Hello,

I really don’t want to split it up. I’ve gotten along very well so far.

Is that right:
So then I can no longer freely switch between e.g. - platform: template, - platform: system monitor, - platform: history_stats?

Greeting Werner

Hallo,

eigentlich möchte ich das nicht aufteilen. Bin bisher so sehr gut zurecht gekommen.

Ist das richtig:
Ich kann also dann nicht mehr zwischen z.B. - platform: template, - platform: systemmonitor, - platform: history_stats frei wechseln?

Gruß Werner

And another question:

For

  - platform: history_stats

there is no new format. Right?

Those are the only ones that have a new configuration format, this change is only for the aforementioned entities of the template platform.
You are using the correct configuration for the history_stats, as you can see comparing it to the docs.

1 Like

Thanks.

Unfortunately I’m too old or too stupid to understand the topic.
I just tried to change my time sensor. Unfortunately without success.

Old:

sensor:
  - platform: template
    sensors:
      datum_zeit_time:
        friendly_name: ""
        value_template: >
          {% set days = ['Mo.', 'Di.', 'Mi.', 'Do.', 'Endl.Fr.', 'Sa.', 'So.'] %}
          {{ days[now().weekday()] }}
          {{ now().strftime('%d.%m. - %H:%M') }}  Uhr

New:

template:
  - sensor:
      - name: "Datum Zeit Time"
          value_template: >
            {% set days = ['Mo.', 'Di.', 'Mi.', 'Do.', 'Endl.Fr.', 'Sa.', 'So.'] %}
            {{ days[now().weekday()] }}
            {{ now().strftime('%d.%m. - %H:%M') }}  Uhr

But it doesn’t work.

Danke.

Leider bin ich zu alt oder zu dumm das Thema zu verstehen.
Habe gerdae versucht meinen Zeit-Sensor umzustellen. Leider ohne Erfolg.

Alt:

Neu:

Funktioniert aber nicht.

Gruß Werner

The current format does not use value_template:, use state: instead. And state: should be at the same indent level as name:.

2 Likes

Thanks very much !!!
I have successes to report.
I’ve already changed some sensors and it worked.

Thank you for your help and patience.

Greetings Werner

Dankeschön !!!
Ich habe Erfolge zu melden.
Habe jetzt schon einige Sensoren umgestellt und es hat funktioniert.

Danke für Deine Hilfe und Gedult.

Liebe Grüße Werner

Unfortunately I have a small problem.

Changed this code to the new format:

Old:


     fensterstatus_schlafzimmer_dg:
       friendly_name: Fenster Wohnzimmer-DG
       value_template: |
         {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
         {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_on_off') %}
         {% if b1 == 'off' and b2 == 'off' %} geschlossen
         {% elif b1 == 'on' and b2 == 'off' %} gekippt
         {% elif b2 == 'on' %} offen
         {% elif b1 == 'unavailable' or b2 == 'unavailable' %} nicht verfügbar
         {% else %} ?
         {% endif %}
       icon_template: |
         {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
         {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_on_off') %}
         {% if b1 == 'off' and b2 == 'off' %} mdi:window-closed-variant
         {% elif b1 == 'on' and b2 == 'off' %} mdi:angle-acute
         {% elif b2 == 'on' %} mdi:window-open-variant
         {% elif b1 == 'unavailable' or b2 == 'unavailable' %} mdi:exclamation
         {% else %} ?
         {% endif %}

New:
     - name: "Fenster Wohnzimmer DG"
       state: |
         {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
         {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_on_off') %}
         {% if b1 == 'off' and b2 == 'off' %} geschlossen
         {% elif b1 == 'on' and b2 == 'off' %} gekippt
         {% elif b2 == 'on' %} offen
         {% elif b1 == 'unavailable' or b2 == 'unavailable' %} nicht verfügbar
         {% else %} ?
         {% endif %}
       icon_template: |
         {% set b1 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_007cf906_on_off') %}
         {% set b2 = states('binary_sensor.lumi_lumi_sensor_magnet_aq2_698aff06_on_off') %}
         {% if b1 == 'off' and b2 == 'off' %} mdi:window-closed-variant
         {% elif b1 == 'on' and b2 == 'off' %} mdi:angle-acute
         {% elif b2 == 'on' %} mdi:window-open-variant
         {% elif b1 == 'unavailable' or b2 == 'unavailable' %} mdi:exclamation
         {% else %} ?
         {% endif %}   


But then I get the following message:

* Invalid config for [template]: [friendly_name] is an invalid option for [template]. Check: template->sensor->7->friendly_name. (See /config/configuration.yaml, line 934).
* Invalid config for [template]: [icon_template] is an invalid option for [template]. Check: template->sensor->7->icon_template. (See /config/configuration.yaml, line 934).

Can you please help me again?

Greeting Werner

I got it - see above.
Thanks very much.