How do I show timestamps for the two states of the one switch?

I want to display timestamps for the two states of the one switch (switch.boiler) on my boiler Lovelace card.

Boiler last off: (timestamp dd.mm hh:mm)
Boiler last on: (timestamp dd.mm hh:mm)

I’m not sure how to do this, hopefully it is straightforward?

Once boiler change state try to write the timestamp to two different input_datetime:

Yes, that’s the part I’m trying to work out how to do

I’m not at a workstation so treat this as pseudo code and the spacing as merely indicative :rofl:

automation :
  - alias: au_boiler_last_on
    trigger:
      - platform: state
        entity_id: switch.yourboilerswitch
        to: 'on'
        from: 'off'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.boiler_last_on
        data_template: "{{ as_timestamp(now())|timestamp_custom('%Y-%m-%d' ~ ' ' ~ '%H:%M:%S' ) }}"

Then copy that, change the ‘alias’ and the on bits to off (and visa versa) to create the ‘off’ one

Perfect, thank you, I’ll try it tonight when I get home from work and tweak to suit where required. No doubt I’ll find other uses for it

No problem, by the way this is ‘almost exactly’ what the link @pniewiadowski pointed you to in the documentation.
Try search for other threads on your topic, and then search the documentation (goto integrations and search from there) failing all that feel free to ask of the community.

By the way please read the “guidelines” (search) and Tinkerer’s very good sticky thread at the top of the forum, it will stand you in good stead.

Good Luck

Edit: By the way, this assumes that you have created two input_datetime 's

e.g. : -

input_datetime:
  id_boiler_on_time:
    name: Boiler Last On Time
    has_date: true
    has_time: true
  id_boiler_off_time:
    name: Boiler Last Off Time
    has_date: true
    has_time: true

Well, I’ve been trying this now for almost 5 hours tonight and nothing seems to work, I’ve tried loads and loads of permutations and I can’t get it to like anything. It complains if I use data_template anywhere.
The only thing that worked was getting them to show up on the Lovelace UI. It always changes to ‘[object Object]’: null. I’ve tried in the automations UI and also editing the automations.yaml.

Automation Automation2 Automation3 Boiler UI

Try not to post pictures, they are huge in comparison to text and it’s hard to copy and paste into a template editor to check/diagnose.

From what I see the object bit is your problem but I have never used the automation editor so I’m going to have to transfer to a workstation (unless someone else comes back before then, circa 2-3 hours)

I tried what you gave me editing the automation.yaml and it complained about the use of data_template in the log. I’ll take the pictures posting comment on board, sometimes it says more than words, however I’ll stick to code tags.

The following single automation records when the boiler is turned on and off.

  - alias: 'Boiler state time'
    trigger:
      platform: state
      entity_id: switch.boiler
    action:
      service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.boiler_last_{{ trigger.to_state.state }}
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

This automation requires the names of the input_datetime entities to be in this format:

input_datetime.boiler_last_on
input_datetime.boiler_last_off

I’ll try that tomorrow. I presume this has to be done by editing automation.yaml and can’t be done via the automation UI?

I don’t use the Automation Editor but I believe you are correct. The Automation Editor has certain limitations and can’t be used to create the example I posted above.

- id: on_off_boiler
  alias: Boiler state time
  trigger:
  - entity_id: switch.boiler
    platform: state
  action:
    service: input_datetime.set_datetime
    data_template:
      entity_id: input_datetime.boiler_last_{{ trigger.to_state.state }}
      datetime: "{{ now().strftime('%d-%m-%Y %H:%M:%S') }}"

I can’t get the value of entity_id: input_datetime.boiler_last_ to show dd-mmm-yyyy (%d-%m-%Y) should have done it I thought, however it always shows as yyy-mmm-dd, both in the states checker and lovelace.

States checker shows the following

editable: false
has_date: true
has_time: true
year: 2020
month: 5
day: 17
hour: 13
minute: 41
second: 0
timestamp: 1589719260
friendly_name: Boiler Last On Time

input datetime ONLY shows it that way, if you want it any different you need to create a sensor and format it as you require.

That or just put it in an input text

1 Like