Non working sensor template

This template does not work:
The cases for auto1, auto2 and auto3, never happens, even if the the conditions are met.

The “binary_sensor.dishwasher_running_program” is alive and working, but the last 4 elif statements get true even if “binary_sensor.dishwasher_running_program” of on…

I am making no sense from this. Any ideas?

  - platform: template
    sensors:
      dish_washer_status:
        friendly_name: 'Dishwasher'
        value_template: >-
          {% if is_state('switch.diskmaskin_program_auto1','on') %}
            Done in {{ now() - states('sensor.diskmaskin_remaining_program_time') }}
          {% elif is_state('switch.diskmaskin_program_auto2','on') %}
            Done in {{ now() - states('sensor.diskmaskin_remaining_program_time') }}
          {% elif is_state('switch.diskmaskin_program_auto3','on') %}
            Done in {{ now() - states('sensor.diskmaskin_remaining_program_time') }}
          {% elif is_state('binary_sensor.diskmaskin_door','on') and is_state('switch.diskmaskin_power','off') and is_state('binary_sensor.dishwasher_running_program','off') %}
            Door open/Turned off
          {% elif is_state('binary_sensor.diskmaskin_door','off') and is_state('switch.diskmaskin_power','on') and is_state('binary_sensor.dishwasher_running_program','off') %}
            Door closed/Turned on
          {% elif is_state('binary_sensor.diskmaskin_door','on') and is_state('switch.diskmaskin_power','on') and is_state('binary_sensor.dishwasher_running_program','off') %}
            Door open/Turned on
          {% elif is_state('binary_sensor.diskmaskin_door','off') and is_state('switch.diskmaskin_power','off') and is_state('binary_sensor.dishwasher_running_program','off') %}
            Door closed/Turned off
          {% else %} 
            Unknown
          {% endif %}
        entity_picture_template: >-
          {% if is_state('switch.diskmaskin_program_auto1','on') %}
            /local/auto.png
          {% elif is_state('switch.diskmaskin_program_auto2','on') %}
            /local/auto45.png
          {% elif is_state('switch.diskmaskin_program_auto3','on') %}
            /local/auto65.png
          {% elif is_state('binary_sensor.diskmaskin_door','on') and is_state('switch.diskmaskin_power','off') and is_state('binary_sensor.dishwasher_running_program','off') %}
            /local/doorOpenpowerOff.png
          {% elif is_state('binary_sensor.diskmaskin_door','off') and is_state('switch.diskmaskin_power','on') and is_state('binary_sensor.dishwasher_running_program','off') %}
            /local/doorClosedpowerOn.png
          {% elif is_state('binary_sensor.diskmaskin_door','on') and is_state('switch.diskmaskin_power','on') and is_state('binary_sensor.dishwasher_running_program','off') %}
            /local/doorOpenpowerOn.png
          {% elif is_state('binary_sensor.diskmaskin_door','off') and is_state('switch.diskmaskin_power','off') and is_state('binary_sensor.dishwasher_running_program','off') %}
            /local/doorClosedpowerOff.png
          {% else %}
            /local/unknown.png
          {% endif %}

Try this in the developer tools template editor and see what result you get.

{{ now() - states('sensor.diskmaskin_remaining_program_time') }}

great, thanks. Now the selection works when I removed that value.

However how do i calculate time left from sensor that have the finish time time_stamp?

sensor.diskmaskin_remaining_program_time keeps the finish time, like: 2020-05-27T12:45:34.263252+00:100:

{{ (states.sensor.diskmaskin_remaining_program_time.state) - now() }}

do not work either…

In seconds:

{{ as_timestamp(states('sensor.diskmaskin_remaining_program_time')) - as_timestamp(now())  }}

In minutes

{{ (as_timestamp(states('sensor.diskmaskin_remaining_program_time')) - as_timestamp(now()))/60  }}

In hours:

{{ (as_timestamp(states('sensor.diskmaskin_remaining_program_time')) - as_timestamp(now()))/360  }}

There’s a post in my mess of bookmarks that will give you a sensible “H hours MM minutes SS seconds” result. I’ll see if I can find it for you.

EDIT: actually that wont work either. Your sensor is in the wrong format. It needs the ‘T’ replaced with a space and the last two characters trimmed off.

ok, thanks alot. I am very close the solution. But of course the time format from dishwasher and Now() is not the same…

now(): 2020-05-27 12:35:34.932049+02:00

dishwasher: 2020-05-27T12:45:34.740915+00:00

The calculation in time left becomes wrong. The missing “T” does not matter, but the the dishwasher is believing that it is in a different time zone, give as error of 2 hours…

Try this

{{ (as_timestamp(states('sensor.diskmaskin_remaining_program_time')|replace( 'T', ' ' ))  - as_timestamp(utcnow()))/360 }}

So very close now : )

I think time zones and day light saving is messing with me:

{{ (as_timestamp((states.sensor.diskmaskin_remaining_program_time.state)[0:32]|replace( 'T', ' ' )) - as_timestamp(now())) | timestamp_custom('%H:%M') }}

now(): 2020-05-27 12:52:26.502399+02:00
diskmaskin_remaining_program_time: 2020-05-27T13:45:34.740915+00:00

The result should be 00:53, but is reported as: 03:53

I did a sneaky edit of my post try this:

{{ (as_timestamp(states('sensor.diskmaskin_remaining_program_time')|replace( 'T', ' ' ))  - as_timestamp(utcnow())) | timestamp_custom('%H:%M')  }}

same 3 hours missmatch. If it wasnt for daylight saving, I could have just substacted 3 hours…

{{ (as_timestamp(states('sensor.diskmaskin_remaining_program_time')|replace( 'T', ' ' ))  - as_timestamp(utcnow())) | timestamp_custom('%H:%M', false)  }}

That removed the daylight saving. Great.
Last thing to remove the time difference of 2 hours. That was harder then I thought.

But


{{ (as_timestamp(states('sensor.diskmaskin_remaining_program_time')|replace( 'T', ' ' )) - (as_timestamp(utcnow())) - 60*60*2 )| timestamp_custom('%H:%M', false)  }}

finally did it! Thanks alot for the help!