Template, return state as datetime

Tags: #<Tag:0x00007fc401f44198>

Hello,

I’m using a template as the Smartthing washer I’m integrating in HA is “missbehaving” for the remaining time.
It is always restarting a “3 hours” countdown while the machine is off. So I did a template like so (in configuration.yaml):

sensor:
  - platform: template
    sensors:
      samsung_washing_time:
        friendly_name: 'Remaining time'
        value_template: >-
          {% if is_state('sensor.washer_job_state', 'none') %}
            {{ "-" }}
          {% else %}
            {{ states('sensor.washer_completion_time') }}
          {% endif %}

And here is the issue.
When the machine is not running, it is displaying the correct ‘-’
But when before, the entity sensor.washer_completion_time was showing as “1 hour 26 minutes”, now it is showing as "2021-07-27T17:48:57Z’

which is not what I wanted to do in the template, I wanted to return the “usual, recognized by HA” value if the other sensor’s value is not ‘none’

Should I remove the states(’’) surrounding the sensor’s value? If so, how?
Maybe using a unit_of_measurement? Which one? I don’t know, I’m lost.

Thank you for the help you can provide.

You could add device_class: timestamp to have “in x hours”, but then “-” will show as “Invalid timestamp”

A compromise can be

sensor:
  - platform: template
    sensors:
      samsung_washing_time:
        friendly_name: 'Remaining time'
        value_template: >-
          {% if is_state('sensor.washer_job_state', 'none') %}
            -
          {% else %}
            in {{ (states('sensor.washer_completion_time')|as_timestamp - now()|as_timestamp) |timestamp_custom("%-H:%M") }}
          {% endif %}

which will give something like in 3:44

1 Like

The “-” is not mandatory, if I don’t put that if path, what will be the rendering with device_class: timestamp then? I’ll try

sensor:
  - platform: template
    sensors:
      samsung_washing_time:
        device_class: timestamp
        friendly_name: 'Remaining time'
        value_template: >-
          {% if not is_state('sensor.washer_job_state', 'none') %}
            {{ states('sensor.washer_completion_time') }}
          {% endif %}

at next washing cycle.

Or it will keep the last value maybe (which I don’t want)?

Anyway, thank you for the help, this is already a major improvement to the ugly ISO 8601 format.

Maybe you can add an “availability” template. It should then show unavailable if there is no machine running, which makes sense.

Solved thanks to @koying

I like it (like that)! (Cardi B, Bad Bunny & J Balvin)

sensor:
  - platform: template
    sensors:
      samsung_washing_time:
        friendly_name: 'Remaining time'
        device_class: timestamp
        value_template: >-
          {{ states('sensor.washer_completion_time') }}
        availability_template: >-
          {%- if is_state('sensor.washer_job_state', 'none') %}
            false
          {% else %}
            true
          {%- endif %}  

I’m in a hurry to see the timestamp part, at next washing time.

Edit: with remarks from next post.

Don’t forget the “else true” on the availability.
And you don’t need the if on the value, this way. The value shouldn’t be read if the entity is unavailable.

I did the same for the step sensor (wash, rince, spin, …) and with the availability, the history is clean, thank you for the tip.

image

Sorry, it is in French :wink:

Pas un problème :wink:

And otherwise, it is working like before for the remaining time, I’m very happy with the latest configuration.
image

1 Like

Is Samsung reading our posts?
Now they are partly sending unavailable instead of funy statuses…
Still sending unrelevant timestamps though