Template, return state as datetime

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

Be lucky if they don’t just straight up remove your washer and dryer entities from being exposed to HA at all. I spend $2K+ on Washer and Dryer and I am literally only able to control them through the stupid Smart Things app. I get 0 automations in Smart Things, so I can’t even use it to create a virtual switch entity and expose that. Nope. Never buying another Samsung Appliance. Honestly considering ditching the phone too. I have paid that company too much money for them to REMOVE the entities I had with an update. I’m sure updates are supposed to improve products, not make me regret going with the company in general.

It is still working, for now, knock on wood.
But I have to admit that for the price, removing functionality is pain in the a$$
The SmartThing integration is really not the best but as I did not buy the machine for that specific functionality, I’m happy with what I managed to get at the end, including a graph of my consumption, like this one:

Lol, rub it in huh? Just playin.
I did buy mine for the functionality, which made it worse. I had it for about 6 months. I had a routine that would allow it to be triggered at 8 pm if smart switch was on. Our electric bill in Texas was set up so that between 8PM and 6AM was free. So, we’d throw a load in, not start it, and let it start itself. Was so convenient.