Been staring myself blind here…
I have two sensors containing a time value in the format HH:MM which I am trying to subtract from each other. This doesn’t work as the result of the template is 0 and not the actual result I am expecting of the sensors subtracted.
I am using the template editor, and it looks like this:
ValueError: Template error: as_timestamp got invalid input ‘20:00’ when rendering template ‘{{as_timestamp(states(‘sensor.cradle_off_time’))|float - as_timestamp(states(‘sensor.cradle_on_time’))|float}}’ but no default was specified
Hi Pieter
I feel that I quite close to something here. Just can’t grasp it completely.
Can you elaborate on where I would suffix the string with ~’:00’ ?
You can probably help me with the following too.
I would like to subtract current time from the sensor “sensor.cradle_off_time” last changed state to know for how long the state has been changed, in the format HH:MM. Currently the .last_changed gives me something like “1 hour ago” instead of 01:00 as I wish.
That results in: 0:08:56.264515
I thought there was a one-liner to omit “:56.264515” and add a character in the hour-field to result in: 00:08, which was my goal. But anyway your way of forcing it works great, it just adds more lines. But I guess that’s the way of doing it then.
My last goal now is to subtract the time when my babys cradle is off with the on time, to show the length of his nap.
Currently I have built the sensors to show the timestamp when the cradle was either on or off in two ways, which currently both works to show the time in HH:MM. You might be able to tell me which sensor is the easiest to work with, in order to subtract the timestamps.
type 1 (template under configuration.yaml)
- trigger:
- platform: state
entity_id: binary_sensor.cradle_in_use
to: 'off'
sensor:
- name: cradle_off_time
state: "{{ now().timestamp() | timestamp_custom('%H:%M') }}"
type 2 (template under configuration.yaml. Updated via an automation which triggers on binary_sensor.cradle_in_use state change)
- sensor:
- name: cradle_off_time_test_sensor
# device_class: timestamp (sensor not working if this line is not commented)
state: "{{ state_attr('input_datetime.cradle_off_time_test', 'timestamp') | timestamp_custom('%H:%M', false) }}"
icon: "mdi:calendar-clock"
The cradle_on_time sensors looks the same as above.
Which of the two types would be easiest to subtract and display as HH:MM ?
Eg on time: 01:00, off time: 02:15, nap: 01:15.
I’ve been thinking about my lastest reply about the creation of the nap time duration sensor.
I believe I could just reuse your proposal for the value template to force-show sensor as HH:MM, as I now have a “baby asleep for” sensor based on that.
I would then have to save the HH:MM as a sensor value right before the cradle is turned off (indicating the nap has ended). I have an idea of how to do that using a automation.
What would be the best way of saving the HH:MM from the sensor, to display the value in my entity-card also as HH:MM? I am thinking of writing it to an input_datetime, but I think It has to have the format of HH:MM:SS to do that ? there might be a smarter way to store it for display ?
General comment:
All of this is basically working around the built-in mechanisms, since one has no option to display a time without seconds. Ideally, you’d store the full datetime object. Don’t confuse data with presentation. One should store the full thing and then display it as needed. For example, the entities card (but not the entity card) has an entity format option to display date, time, datetime, etc. One can display the input helper directly. One can also set the helper to store only a date, time or datetime.
Thanks. Will try input_text instead !
And thanks for the comment. It makes sense. I will try to adapt my HASS to it. Still a learning curve for me here