Report date and time of NODE-RED flow in Home Assistant

I have build several NODE-RED flows during the last months. I would like to know if there is a possibility to display the date and time when a specific flow has run in Home Assistant.

At this moment, I monitor the devices which are connected to these flows and report the date and time when they are switched on. The issue with this setup is that the date and time will be updated after a restart of Home Assistant, which resulting in incorrect information.

Therefore I am looking for a solution which displays the execution date and time of the NODE-RED flow. Thanks in advance for your reaction.

I use the log service for this. Then it shows up in you HA log with everything else.

[{"id":"ed70754a.05e94","type":"inject","z":"84b1cc62.4053","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":3080,"wires":[["ba40d028.d6404"]]},{"id":"ba40d028.d6404","type":"template","z":"84b1cc62.4053","name":"Logbook Formatting","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n    \"data\":{\n            \"name\":\"Node Red Flow\",\n            \"message\": \"has been triggered\",\n            \"entity_id\": \"automation.fake\",\n            \"domain\": \"automation\"\n    }        \n}\n\n","output":"json","x":280,"y":3160,"wires":[["8764fd35.c6022"]]},{"id":"8764fd35.c6022","type":"api-call-service","z":"84b1cc62.4053","name":"Write to Logbook","server":"22e46917.4b888e","version":1,"debugenabled":false,"service_domain":"logbook","service":"log","entityId":"","data":"{}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":270,"y":3220,"wires":[[]]},{"id":"22e46917.4b888e","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

You can always go the route of updating a fake sensor you create too if you want to have it in the UI.

1 Like

Hi Chris,

Thanks for your help! I have imported your flow in my NODE-RED, however, when I use the timestamp button, I get a message “No connection” in the “write to logbook” node. Do you know what to do?

Did you add your HA Server to the Call Service node?

1 Like

You can just create an input_datetime or inout_text and use call service node to set the tex/time from Node red.
I use something similar in one of my flows.

1 Like

True, but the “no connection” error tells me @cmartens didn’t set his HA server in the call service node. He needs to fix that first.

1 Like

Hi, it is working now! The message is displayed in the Logbook. The next step would be that this data and time are also displayed in the UI without being changed as Home Assistant will be restarted. Is this possible?

As stated previously, save it in a date time or text entity.
Use the call service node and set it.

Do you have an example? I don’t know how to do this.

Something like this.
But you need to change the entities to what you have in your Home Assistant.


[{"id":"4ebefd89.d69164","type":"function","z":"7129ee98.085df","name":"","func":"var d = new Date(); \nmsg.time =  d.getFullYear() + \"-\" + ('0' + (d.getMonth()+1)).slice(-2) + \"-\" + d.getDate() + \" \" + ('0' + (d.getHours())).slice(-2) + \":\" + ('0' + (d.getMinutes())).slice(-2) + \":\" + ('0' + (d.getSeconds())).slice(-2);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2360,"y":1140,"wires":[["fffa678.7820998"]]},{"id":"fffa678.7820998","type":"api-call-service","z":"7129ee98.085df","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"input_datetime","service":"set_datetime","entityId":"input_datetime.test","data":"{\"entity_id\":\"input_datetime.test\",\"value\":\"{{time}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2600,"y":1140,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","z":"","name":"Home Assistant","addon":true}]

Thanks! I have imported this flow. I have added the following entry in configuration.yaml:

input_datetime:
  test123:
    has_date: true
    has_time: true

However, I am not able to select “input_datetime.test123” as entity in Node-RED. This entity is well defined, however it looks that Node-red is not able to use or see it.

Do you have any clue how this can be solved?

Is this correct?

image

yes, I have changed this entry to the first “Home Assistant” entry in the list, like all other node-red flows.

Did you try to restart?

Hi, performed a restart and after some waiting the entity popped up in Node-red. Now I get an API error:

Call-service API error. Error Message: extra keys not allowed @ data[‘value’]

I have removed the contents from the Data section. The API error is gone now. However, the entity is not updated when I use the flow

Sorry… My bad.
The json is incorrect. It says value: … It should be datetime: …

Changed the code to “datetime”, however the API error still exists…

This is tested and working


[{"id":"4ebefd89.d69164","type":"function","z":"7129ee98.085df","name":"","func":"var d = new Date(); \nmsg.time =  d.getFullYear() + \"-\" + ('0' + (d.getMonth()+1)).slice(-2) + \"-\" + d.getDate() + \" \" + ('0' + (d.getHours())).slice(-2) + \":\" + ('0' + (d.getMinutes())).slice(-2) + \":\" + ('0' + (d.getSeconds())).slice(-2);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2360,"y":1140,"wires":[["fffa678.7820998"]]},{"id":"fffa678.7820998","type":"api-call-service","z":"7129ee98.085df","name":"","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"service_domain":"input_datetime","service":"set_datetime","entityId":"input_datetime.test","data":"{\"entity_id\":\"input_datetime.test\",\"datetime\":\"{{time}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2600,"y":1140,"wires":[[]]},{"id":"e4e32bcd.1177b8","type":"inject","z":"7129ee98.085df","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2210,"y":1080,"wires":[["4ebefd89.d69164"]]},{"id":"4bbca37b.1700ec","type":"server","z":"","name":"Home Assistant","addon":true}]

The good news is that the flow is working now. Thanks! The “input_datetime.test” entity is updated when I use the Node-red flow. However, when I restart Home Assistant after updating the entity, the entity is set back to 01-01-1970 again, I would like that the entity value will be kept after restarting Home Assistant. This should be possible according to the “Input DateTime” documentation.