How can I create a template sensor by taking the following 2 criteria into account.
I will know the end times how long the irrigation will run.
I have a scheduler card where watering starts at 4:30 am and a sensor with the total watering time.
So the watering ends at 6:30 a.m.
I want to combine the two things
Exactly what I asked for too, but not given, so who knows.
I think you’ve nailed it. This is probably the (incorrect) expectation.
@Kabala sensor values are immutable in templates. You can assign the result to a variable or use the output of a template to set a template sensor’s value, but you can’t change the existing value in place.
2h 0min is not an integer, so you can’t convert it to an integer. This will get complicated if the format changes based on the time. To be honest, tha’ts not a normally formatted attribute for a time and because of this it makes it harder than normal to use.
{% set t = state_attr('switch.schedule_0ce9ef', 'next_trigger') | as_datetime %}
{% set h, m = states('sensor.zone_gesamt_time_total') | regex_findall('^(?:(?P<h>[0-9]{1,2})h ){0,1}(?:(?P<m>[0-9]{1,2})min){0,1}') | first | default(['','']) | map('float', 0) %}
{{ t + timedelta(hours=h, minutes=m) }}
I would consider rebuilding the template for sensor.zone_gesamt_time_total. It can just sum seconds and output that, with the appropriate device class and UoM. Everything else will then be simpler, including using it with the timedelta function.
hi Petro,
thanks for the configuration, works very well.
I still have two questions.
How can I display the date format in iso?dd/mm/yyyy
And the Coordinated Universal Time ( UTC ) how can I hide that?
It’s already output in iso format, which is yyyy-mm-dd. If you want it output in a different format than that, you can use .strftime
{% set t = state_attr('switch.schedule_0ce9ef', 'next_trigger') | as_datetime %}
{% set h, m = states('sensor.zone_gesamt_time_total') | regex_findall('^(?:(?P<h>[0-9]{1,2})h ){0,1}(?:(?P<m>[0-9]{1,2})min){0,1}') | first | default(['','']) | map('float', 0) %}
{{ (t + timedelta(hours=h, minutes=m)).strftime('%d/%m/%Y %H:%M:%S') }}
Just keep in mind that all these format changes will make these entities harder to use in automations. If you simply stick to normal formats, with typical sensors in HA, the frontend will translate all times into your selected format.
@parautenbach is on the correct path saying that you should rework all your template entities.