Calculate timer duration on known completion time

Hello,
apparently a bit of math. I have a sensor provided by my washing machine that returns the completion time. How can I start a timer with a duration based on this sensor?

1 Like
  - service: timer.start
    target:
      entity_id: timer.your_timer
    data:
      duration: >
        {% set t = states('sensor.your_sensor') | as_datetime | as_local %}
        {{ (t - now()).total_seconds() }}

Replace timer.your_timer and sensor.your_sensor with the entity_id of your actual timer and sensor.


NOTE

If the sensor’s device_class is timestamp, it can be used directly by a Time Trigger.

trigger:
  - platform: time
    at: sensor.your_sensor

Reference:
Time Trigger

1 Like

Great, works like a sharm. It would be great to have a text book for such a simple solutions of home assistant or templating in general.

THANKS!

2 Likes

This is exactly what I’m trying to do but am stuck. I need a hint… where do you put the - service snippet Taras provided?

It is an automation. I need to tweak it a bit but I run it when the job starts. What I need to do is to restart the timer again, when the washing machine recalculates the time as it does like to do it very often. It is making decisions what to do according to various sensors such as clarity of water etc. but with growing number of kids, I have less time for HA

alias: SAMSUNG - Washing machine timer
description: ""
mode: single
triggers:
  - entity_id:
      - sensor.washing_machine_washer_completion_time
    trigger: state
conditions: []
actions:
  - data:
      duration: >
        {% set t = states('sensor.washing_machine_washer_completion_time') |
        as_datetime | as_local %} {{ (t - now()).total_seconds() }}
    target:
      entity_id: timer.washing_machine
    action: timer.start