Display date and time of Node-RED in Home Assistant

Hi,

I have build multiple Node-RED in Home Assistant, which switches on and off several devices automatically. I would like to display the date and time in Home Assistant when the state of a device changes.

In the example below, the device “Pomp” was switched off on 8 April at 09:31h via a Call Service node. Is it possible to display this date and time in the UI of Home Assistant?

Schermafbeelding 2020-04-09 om 22.31.49

Thanks in advance for your reaction.

you could use an API service call but that seems overkill. Why not simply create a Sensor that checks when the device was turned off?

Here is some documentation on the API just in case:


Pretty simple to use via a http node.
1 Like

Hi Marcus,

Thanks for your reaction and help. I have created a sensor called ‘datum_tijd_laatste_beregening’ which should report the date and time when the entity ‘switch.fibaro_fgs222_beregeningspomp’ is enabled. However, the result is an error in the log. I expect that something is not correct in the value_template section, however, I am not able to find the right information on the internet. Do you know what needs to be changed?

  - platform: template
    sensors:
      datum_tijd_laatste_beregening:
        entity_id: switch.fibaro_fgs222_beregeningspomp
        value_template: >-
          {% if states('switch.fibaro_fgs222_beregeningspomp', 'on')}
            {{ now().strftime("%H:%M")}}
          {% else %}
            Undefined 
          {% endif %}```

In the meantime, I have adapted the value_template. The sensor reports now the status of the Entity ‘switch.fibaro_fgs222_beregeningspomp’ on and off. Now I need to know how to replace On and Off by the date & time of the last change.

  - platform: template
    sensors:
      datum_tijd_laatste_beregening:
        entity_id: switch.fibaro_fgs222_beregeningspomp
        value_template: '{{ states.switch.fibaro_fgs222_beregeningspomp.state}}'

Try this:
states.switch.fibaro_fgs222_beregeningspomp.last_changed

1 Like

I have tried ‘states.switch.fibaro_fgs222_beregeningspomp.last_changed’ and it worked! However I had some issues with formatting the date and time. At the end I ended with:

  - platform: template
    sensors:
      datum_tijd_laatste_beregening:
        entity_id: switch.fibaro_fgs222_beregeningspomp
        value_template: >
          {{ as_timestamp(states.switch.fibaro_fgs222_beregeningspomp.last_changed) | timestamp_custom(' %d-%m-%Y %H:%M') }}

This is the result:

Thanks for your help!

1 Like