Output the total on-time of a sensor daily

Hello,

I’m using a window sensor ( binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off ).
This can be either ON or OFF.

Now I would like to know how long the sensor was on for each day (always starting at midnight).

So that I can see that, for example, today (the day is not over yet) it was ON for a total of 2 hours and 44 minutes.
But it should also be possible to call up another day that has already passed.
Example: Yesterday the sensor was ON for a total of 3 hours and 12 minutes.

I hope you understand what I mean and can help me to get this issue.

Thanks so much.

Greeting Werner

Moin Werner, diese Integration sollte dein Problem lösen.
Das genannte Beispiel auf der Seite passt sogar ziemlich genau zu deinem Anwendungsfall.


Greetings Werner, this integration should fit your use case - in fact the provided example should work flawlessly for you.

1 Like

Hello,

great - exactly what I was looking for. Thanks very much.

Greeting Werner

Hello,

I have to ask again. I may not understand the configuration properly.
The aim is to output the total ON time per day.

Example. 1. Today to now, yesterday, the day before yesterday (or by date).

My code now looks like this - but it doesn’t seem to work properly. I only get an output that cannot be correct.

 - platform: history_stats
   name: Fenster Arbeitszimmer Gesamt
   entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_19330507_on_off
   state: "on"
   type: time
   end: "{{ (now().replace(minute=0,second=0) + timedelta(hours=8)).replace(hour=0) }}"
   duration:
       hours: 24

Where could the mistake lie?

Greeting Werner

Use the first provided example, why did you add the timedelta here?
You can DM me if you want to continue in german.

Anyway:
The history_stats sensor can work in a few ways:

  • Give it a start- and endtime and it will evaluate the state within this time period
    • For example: It may start at 07:00 and end at 15:00, so the evaluated time period are the 8 hours within
  • Give it a starttime and a duration and it will start at the starttime for the duration, and evaluate the state within that time period.
    • For example: It could start at 05:00 for a duration of 12 hours.
  • Give it an endtime and a duration. Now it will look backwards beginning from the endtime for the specified duration
    • For example: The timeperiod could end at 19:00, with a specified duration of 6 hours, now it will look at everything between 13:00 and 19:00

That’s fairly easy. Sadly, days come to an end and when that is when your history_stats sensor will change too in the aforementioned cases.
Let’s say we’ve defined a history_stats sensor to look at a time period between 04:00 and 08:00 and the entity we’re observing is always on.

This is how our configured sensor would look like throughout the day:

  • From 00:00 to 04:00 its state is 0
  • Starting from 04:00, the state will increase, by 1 every hour
  • At 08:00 the state will be 4
  • The state will be 4 until 23:59
  • The next day at 00:00 the state will be 0 again, because the observed entity hasn’t been on on that day.

Now, I’ll assume you’ve copy-pasted the “Next 4PM” example and modified it slightly. Please try the very first example in the documentation page, that should work out for you.
Your current template works like this:

  • It will look back for 24 hours.
  • The endpoint is always the current day, so no matter if it’s 00:00 or 11:23, your example will always start to look back from 00:00 of the current day, until 00:00 of the previous day
    • The only exception: After 16:00 the timedelta will move the endpoint into tomorrow (Because 16+8=24), so after 16:00 the sensor will show you the results of 00:00 tomorrow until 00:00 today (instead of 00:00 today until 00:00 yesterday)

I don’t think that makes a lot of sense for your application.

I think this is what you want:

- platform: history_stats
   name: Fenster Arbeitszimmer 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() }}"

^ This will:

  • Always start the observed timeperiod on 00:00 of today
  • End it now

In effect it will give you: How long has the configured entity been on today (this calendar day, not the last 24 hours).

If you wanted it to look back 24 hours, no matter which day of the week it is, you might want something like this:

- platform: history_stats
   name: Fenster Arbeitszimmer Gesamt
   entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_19330507_on_off
   state: "on"
   type: time
   end: "{{ now() }}"
   duration:
     hours: 24

^ This will:

  • End now
  • Look back at the last 24 hours. As you can see, since we don’t use timedeltas or replacements, this will look at those last 24h no matter what time of day it is.

Maybe I’m not doing a good job explaining it :smiley: In any case, good luck with your integration!

1 Like

Hello,

Thank you very much for your detailed answer.
I just keep writing here in two languages ​​- see below.

So your first example works great for the current day.
However, if I now also want to have the values ​​from another day (e.g. yesterday), then this does not seem to work. Since I probably need another code (e.g. for yesterday). Am I seeing this correctly, or can I also see these values ​​after a while (I hadn’t included the code yesterday)?

I would like to see the result:

  • Today the sensor was open x time
  • on another day - e.g. yesterday - y time

Do you understand?

Greeting Werner

Hallo,

vielen Dank für Deine ausführliche Antwort.
Ich schreibe hier einfach in zwei Sprachen weiter - siehe unten.

Also Dein erstes Beispiel funktioniert super für den aktuellen Tag.
Wenn ich jetzt aber auch die Werte von einem anderen Tag haben möchte (z.B. Gestern), dann scheint dies nicht zu gehen. Da muss ich wahrscheinlich einen weiteren Code (z.B. für Gestern). Sehe ich das richtig, oder kann ich diese Werte nach einer Zeit auch sehen (hatte ja gestern den Code noch nicht eingebunden)?

Würde halt im Ergbniss gern sehen:

  • Heute war der Sensor x Zeit geöffnet
  • an einem anderen Tag - z.B. Gestern - y Zeit

Verstehst Du?

Gruß Werner

Hey Werner,

ah, I understand. Since there currently isn’t an easy way to access the state of a sensor yesterday, the easiest thing to do would be to write two history_stats sensors. Instead of recording every day with one history_stats, and then looking back at those recordings, we’ll just use two, as you suggested.

Doesn’t the second example from the docs work?
It would look like this:

- platform: history_stats
   name: Fenster Arbeitszimmer Gesamt Gestern
   entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_19330507_on_off
   state: "on"
   type: time
   end: "{{ now().replace(hour=0, minute=0, second=0) }}"
   duration:
     hours: 24
1 Like

Thank you Fedot!

So that was a problem of understanding. So I can’t just go back to days gone by with this new sensor.
Have now created a second sensor - as you suggested.
Works perfectly.
If it were possible to access all the days saved so far, I would have been able to show a better progression.

Thank you again.

Greeting Werner

Lieben Dank Fedot !

Also das war ein Verständnisproblem. Ich kann also auf diesen neuen Sensor nicht einfach auf bereits vergangene Tage zurück greifen.
Habe jetzt noch einen zweiten Sensor - wie von Dir vorgeschlagen - angelegt.
Funktioniert super.
Wäre der Rückgriff auf alle bisher gespeicherten Tage möglich, hätte ich eine bessere Verlaufsentwicklung darstellen können.

Hab nochmals vielen Dank.

Gruß Werner

Oh maybe I didn’t express myself properly, you can absolutely access all data that’s been recorded and is still present in your Database, depends on how long you let the data live before it gets purged.
What I meant is that you couldn’t easily create a sensor “XYZ heute” and then access the state of THAT sensor from yesterday, yes you can see it in the History tab, but not easily access it within templates.
So you have to create sensors to accompany each use case seperatly. (Also has the upside that you don’t have to wait a few days for data to show up).

If I understand your intention correctly - do you maybe want to draw a graph with this data? Like the history card in Lovelace? Maybe sth. like this is possible after all…

1 Like

Oh great. Then it’s exactly how I wanted it. Now I got it right.

Thank you very much.

Greeting Werner

The sensor works fine.
But I still have a question. Is it possible to change the history from hours to minutes (X-axis)?
See photo.

Greeting Werner

Hey Werner,

no I don’t think you can do that directly within the history_stats sensor.
However, you can easily create a Template Sensor which just multiplies the history_stats sensor by 60.
I haven’t tested it, but this should do the trick:
(I have guessed the name sensor.fenster_arbeitszimmer_gesamt_heute, please verify that this is actually the entity_id of your history_stats sensor, otherwise adapt it to fit :slight_smile: )

template:
  - sensor:
      - name: "Fenster Arbeitszimmer Gesamt Heute Minuten"
        unit_of_measurement: "min"
        state: "{{states('sensor.fenster_arbeitszimmer_gesamt_heute')|int * 60 }}"
1 Like

Hallo und wieder Danke.

Es hat zwar einen neuen Sensor erstellt, er zeigt aber 0 Miniten an, obwohl der eigentliche Wert 0,4 Stunden war.

Habe den richtigen Senor eingetragen, evtl. stimmt etwas nicht mit der Reihenfolge des Codes?

So sieht er aus:

# 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

  - platform: history_stats
    name: Arbeitszimmer 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() }}"

template:
  - sensor:
      - name: "Fenster Arbeitszimmer Gesamt Minuten"
        unit_of_measurement: "min"
        state: "{{states('sensor.arbeitszimmer_gesamt')|int * 60 }}"

Gruß Werner

I get the following error message:

Invalid config for [template]: [platform] is an invalid option for [template]. Check: template->platform. (See /config/configuration.yaml, line 252).

You’ve configured some of the sensor in the legacy format, make sure you didn’t mix up anything, see the bottom of the docs

I’d recommend using the newer format.

1 Like

Oh…
That presents me with real challenges.
Unfortunately, I’m a beginner and have always expanded my system step by step.
I probably won’t be able to make the change.
how could i get the code

template:
  - sensor:
      - name: "Fenster Arbeitszimmer Gesamt Minuten"
        unit_of_measurement: "min"
        state: "{{states('sensor.arbeitszimmer_gesamt')|int * 60 }}"

because display in the old format?

Greeting Werner

At some point Home Assistant might stop supporting the legacy format, a few versions before such a change you’ll probably receive warnings under the “Repair” section. You don’t have to migrate to the new format now, but you probably shouldn’t write more legacy code either.

The sensors you’ve posted are easy to migrate. Take a look at the docs, open them in 2 windows side-by-side and compare the code formats. For most configs, you basically just swap “template” and “sensor”.

The code you’ve posted here looks valid, but not under a sensor: block. Did you paste that code in sensor.yaml?
If so, cut it there and paste it - either in configuration.yaml with a template: in front, like in your example, or just create a new file e.g. " template.yaml", which you then include like this:
template: !include template.yaml

1 Like

Hello again,

I’ll leave the German version under this answer.
So now I understand that I don’t have to change everything to the new format right away. That means that the old and new format can also be used together.
However, the new code caused an error.
So I tried to use the old format for my current problem first.

The code now looks like this and no longer brings any errors. Nevertheless, the output is “0 min”, although the actual sensor already shows “0.05 h”:

# 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')|int * 60 }}"

Where else could the problem lie?
By the way, I have everything in the configuration.yaml

But now back to the topic of the new format. I would like to convert my old format, but I don’t know where to start.
It didn’t work with the example of the minute sensor. Why not? That was the new code. I’m really overwhelmed. sorry
What’s the best way to start?

Example courtyard gate control:
It currently looks like this:

# Hoftorschalter
 - platform: template
   switches:
     hof_door:
       friendly_name: "Hoftorschalter"
       value_template: "{{ is_state('binary_sensor.hoftor_sensor_ias_zone', 'off') }}"
       turn_on:
         service: light.toggle
         entity_id: light.mr02_modul_relais_on_off
       turn_off:
         service: light.toggle
         entity_id: light.mr02_modul_relais_on_off
       icon_template: >-
         {{ 'mdi:gate' if is_state('binary_sensor.hoftor_sensor_ias_zone','on') else 'mdi:gate-open' }}

can you tell me how this entry should look like in the new format?

Please excuse me, I’m really on the hose there.

Greeting Werner

Hallo nochmal,

ich lasse unter dieser Antwort mal die deutsche Version stehen.
Also ich habe es nun so verstanden, dass ich nicht gleich alles in das neue Format ändern muss. Das bedeutet ja, dass altes und neues Format auch zusammen verwendet werden darf.
Dennoch hat der neue Code ja einen Fehler verursacht.
Daher habe ich versucht ersteinmal das alte Format für mein aktuelles Problem zu nutzen.

Der Code sieht nun so aus uns bringt auch keinen Fehler mehr. Dennoch kommt als Ausgabe “0 Min”, obwohl der eigentliche Sensor schon “0,05 h” anzeigt:

# 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')|int * 60 }}"

Wo könnte da noch das Problem liegen?
Ich habe bei übrigens alles in der configuration.yaml

Nun aber nochmals zum Thema des neuesn Formates. Ich würde gern mein altes Format umstellen, weiß aber nicht wo ich anfangen soll.
Am Beispiel des Minuten-Senors hat es ja nicht geklappt. Warum nicht? Das war doch der neue Code. Bin da echt überfordert. Sorry.
Wie fange ich am besten an?

Beispiel Hoftorsteuerung:
Diese sieht derzeit so aus:

# Hoftorschalter
  - platform: template
    switches:
      hof_door:
        friendly_name: "Hoftorschalter"
        value_template: "{{ is_state('binary_sensor.hoftor_sensor_ias_zone', 'off') }}"
        turn_on:
          service: light.toggle
          entity_id: light.mr02_modul_relais_on_off
        turn_off:
          service: light.toggle
          entity_id: light.mr02_modul_relais_on_off
        icon_template: >-
          {{ 'mdi:gate' if is_state('binary_sensor.hoftor_sensor_ias_zone','on') else 'mdi:gate-open' }}

kannst Du mir sagen wie dieser Eintrag im neuen Format aussehen müsste?

Bitte entschuldige, da stehe ich wirklich auf dem Schlauch.

Gruß Werner

Alright, so the main reason why your template sensor doesn’t work is because I’m an idiot.

Please try this code instead:

  - 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 }}"

If that works, we can start working on migrating your existing config.

1 Like

hello again

thank you so much for your hard work and help. German again below…
Now the sensor works.

I would like to do the migration.
Would it be possible for you to show me how the code should look after the change using the above example?
So maybe using the example of the courtyard gate switch, but also the example just worked on for the minute conversion.

I hope I don’t have to migrate everything at once. Little by little I’ll get there for sure.

Thank you and best regards Werner

Hallo mal wieder,

vielen, vielen Dank für die Mühe und Hilfe. Deutsch wieder unten…
Jetzt funktioniert der Sensor.

Die Migration würde ich gern durchführen.
Wäre es möglich, dass Du mir am o.g. Beispiel zeigst wie der Code nach der Umstellung ausshen müsste?
Also evtl. am Beispiel Hoftorschalter, aber auch am gerade bearbeitetn Beispiels für die Minutenumrechnung.

Ich hoffe ich muss nicht alls auf einmal migrieren. Nach und nach bekomme ich das sicher hin.

Danke und liebe Grüße Werner