I have build a little temperature and humidity sensor with D1mini and Tasmota.
I am using automations and mqtt to set it to deep sleep. This is all working fine.
For more details: Link
I would like to add a sensor, which should indicate, how long the current DeepSleepTime is.
When changing the DeepSleepTime via mqtt, Tasmota sends back a confirmation:
As there is no āDeepSleepTimeā-object in the second json, my above sensor throws an error.
I am looking for a way to have something like a trigger-based sensor, which only updates, if the json-object āDeepSleepTimeā exists. How do I check this in a template?
@123
Thank you very much for your quick help. I played around with āis definedā, but got always the error, that the attribute āDeepSleepTimeā is not defined, or something like this.
I will test your template tomorrow evening[*] and report back, but I am confident that it will work.
As I do love regex, here the template, I came up with just a minute ago in the dev tools.
- name: "Wohnzimmer DeepSleepTime"
unique_id: "mqtt.Wohnzimmer.DeepSleepTime"
state_topic: "stat/D1mini/Wohnzimmer/RESULT"
value_template: >
{{ value_json.DeepSleepTime | timestamp_custom("%H:%M", False, "unavailable") if value_json.DeepSleepTime is defined else this.state }}
device_class: "duration"
unit_of_measurement: "h"
As soon, as the DeepSleepTime is 3600s ā I get ā01:00ā. If it is one of the others, I get ā0ā. This comes probably from the UoM. (I added the UoM because the log was complaining about the missing configuration for the ādurationāā¦)
Could I change the template to show something like this:
5 min
15 min
1 hour (or if easier: 60min)
thanks,
Chris
p.s.: where do I find the documentation for all that time-related stuff? I search on the homeassistant documentation, I search for jinja2, ā¦ but I couldnāt find anything helpfulā¦)
ok, I found the ādurationā: Sensor - Home Assistant
If you specify the unit of measurement to be āhā then the result produced by value_template must be a float or int (i.e. numeric). You canāt have the template report 00:30 if you are using h as the unit of measurement. In other words, if the value of DeepSleepTime is 1800 then the template should report 0.5 (half an hour).
If you want it to report the time as text that includes āminā and āhourā then I donāt believe you can set the sensorās device_class to be ādurationā or specify a unit of measurement.
If you remove device_class and unit_of_measurement, you can try this and see if it meets your needs: