Show time since service call

I have a service call to calibrate my CO2 sensor after opening the window for ventilation using mhz19_calibrate_zero, Now I want my dashboard to show the time since the last recalibration. I know I need something like what I found in another thread:

- platform: template
  sensors:
    rosie_slaap_timer:
      friendly_name: Rosie slaap timer
      icon_template: mdi:clock-outline
      value_template: >
        {% set t = states('sensor.time') %}
        {{ relative_time(...) }}"

But I have no idea what to put into relative_time(). Can you help?
Danke

This is what I use for my template sensors to help me print a pretty Last Update time on my camera snapshots.

{% set timestamp = states.camera.front_door_ring_snapshot.attributes.timestamp | int | as_datetime() | as_local %}
      Last Update: {{ timestamp.strftime('%b %-d, %Y %-I:%M %p') | default('N/A') }}

but I believe now() should give you a current date/time in your current time zone

Yes, devices may have states you can latch onto. In my Esphome I have:

# Enable Home Assistant API
api:
  password: ""
  services:
    - service: mhz19_calibrate_zero
      then:
        - mhz19.calibrate_zero: sensorid

and my automation is:

alias: Raum-6-Calibrate-CO2
description: ""
trigger:
  - platform: state
    entity_id:
      - timer.lueften
    to: active
    for:
      hours: 0
      minutes: 12
      seconds: 0
condition: []
action:
  - service: esphome.a03_mhz19_calibrate_zero
    data: {}
mode: single

There, in the automation I’d need to set some timestamp to now() and place that into the relative_time() in the dashboard. Only I do not know the correct syntax.

Well I think I may not fully understand relative_time - however this is how I set a datetime helper in my automations/scripts:

- service: input_datetime.set_datetime
  target:
    entity_id: input_datetime.test_datetime_helper
  data:
    datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

According to what I’m understanding is you want to set a timer. This is how I set my timers, I just use seconds but it wouldn’t be hard to modify for minutes/hours. I have an input_number helper that changes based on sun.sun so during the day my light timer is 15 minutes at night it trims to 7 minutes. I make that into seconds and use it here. but the format for duration is 00:00:00 if you don’t include any : it defaults to seconds, if you want to set minutes then 05:00 would be 5 minutes, hours then 01:05:00 would be 1 hour 5 minutes. (Again I prefer seconds, easier to work with helpers)

          - service: timer.start
            target:
              entity_id: timer.dressing_room_timer
            data:
              duration: "{{ (states.input_number.lightmotiondelay.state | int ) }}"

Then to display on a dashboard, I would use an entities card, there might be better options, perhaps a mushroom entities card? Look at options there. But this works and regardless of how you set your time this will show in relative time hh:mm:ss or it will say Idle.

type: entities
entities:
  - entity: timer.bathroom_fan_timer
  - entity: timer.bathroomtimer
  - entity: timer.diningroomtimer
  - entity: timer.kitchentimer

Screenshot 2024-03-26 083426

No absolutely not. I use timers, they run backwards for a set amount of time. What i want to do here is store (and reset) a fixed point in time and show how long ago it was. But your

- service: input_datetime.set_datetime

seems to be the way to go. I’ll try that and report back.

1 Like

if i could jump in…

let me verify what i think you’re asking. you have an automation that calls esphome.a03_mhz19_calibrate_zero. you want a sensor to tell how long it’s been since that automation was run. is that right?

if so, that shouldn’t be too hard. if your automation is called automation.raum_6_calibrate_co2 then your sensor should be something like:

- platform: template
  sensors:
    rosie_slaap_timer:
      friendly_name: Rosie slaap timer
      icon_template: mdi:clock-outline
      value_template: >
        {{ now() - state_attr('automation.raum_6_calibrate_co2', 'last_triggered') }}

note that tihs will give the amount of time that has passed since that automation was called. if someone calls esphome.a03_mhz19_calibrate_zero directly, it won’t catch that.

you don’t need to store away a separate date time helper because the automation already stores the last time it was run.

shoot, i keep forgetting to hit the right reply button. see :point_up_2:

With your help I nailed it – not perfect but good enough. Contrary to my expectations both a duration and relative_time() yield not a number but a string, “8:26:10.12545” and “8 hours” respectively and neither of them nice. So I reverted to showing the absolute time. I have a templates.yaml but for small things I prefer the UI, so I can’t show the solution in full. The template I use and display is:

"{{ state_attr('automation.r6_calibrate_co2', 'last_triggered').strftime('%a %H:%M') }}"

giving me “Tue 11:53”.

BTW I learnt something else important. When I write a new automation and then rename it, the new name is only an alias. It is the first name with all its typos that will be retained forever in the id. Thus the R6 vs. Raum-6 mixup. I’ll try to look out for that mistake in future.

the time elapsed that i had above is easily converted to many different formats, numbers and different presentations…

Correction:
The time given is in GMT. I just amended the template to:

{{ as_local(state_attr('automation.r6_calibrate_co2', 'last_triggered')).strftime('%a %H:%M') }}

Yes thank you. I’m currently reading the templating documentation. There is still a lot to learn.