I have a sensor that tracks my baby’s sleep amount, however, I’m unable to do so because the sensor might start at 11 pm and end at 1 am the next day. So I should get a value for 1hr today and 1hr for tomorrow. The thing is the sensor goes off when I log it and i may forget to log the time till the next day hence why I need the template to go off the the Attributes timestamps Start & End. See image
I have no skill in this so I have been trying to use Chat GBT, but none of the examples are working.
Maybe this will help figuring it out. The variables start and end represent the attributes of your sensor.
{%set start = today_at("23:00") - timedelta( days = 1) %}
{%set end = today_at("01:15") %}
{% if start.date() != end.date() %}
Slept yesterday: {{ today_at("00:00") - start }}
Slept today: {{ end - today_at("00:00") }}
{% else %}
Slept today: {{ end - start }}
{% endif %}
This should result in:
Slept yesterday: 1:00:00
Slept today: 1:15:00
Replace the current ‘end’ with {%set end = today_at("23:15") - timedelta( days = 1) %} and see the result if start and end are on the same day. The result:
Thanks, mate, im not sure what this is telling me or how I use this
I have this value template which gives me 2.9 for the last state which is a 171/60, however, that’s not the duration of the attribute, its 2hrs and 51 mins… I’m lost and thank you again for helping.
can you explain when the sensor updates and where it comes from? There’s a number of ways to achieve this and we want to choose the easiest path. Using a template will be painful.
Hey Petro, so im using Buddy Baby and you add database entries for Sleep, feeding etc.
There is a sensor entity for each of these activities. Below is for feeding.
The potential issue is I can forget to add say a feed session that was for 10pm-11pm until the next morning. If I were to use Utility Meter that feed is counted for the day and not based on the Attributes Start and End.
yeah it updates 1 min later with what I add in Buddy baby.
So if I add a feed session and add the volume of 90 MLS, it then updates sensor.beau_wilder_last_feeding main state to 90ml and updates all the attributes for that sensor
If that’s the case, you can just make a template sensor that updates via a trigger. Adding the time to some input helpers and then reset the input helpers at midnight. Or you can attempt to make this into template entities, which would behave similar. I can help you make those if you’re interested.