History/Logging in node red

I started using node red in place of automations and love it, but one issue is knowing what turned a light on. What do you have for logging or history?

The first code below is an example of writing to a log file, and the second for writing to the HA Logbook. I use the first when I need to collect data over a longer time frame, and the second so I can quickly check to see when an event happened. I don’t have any examples, but you should be able to check the complete msg object to determine what user initiated an event, or at least log the fact that it came from your Node Red flow.

[{"id":"740fac5f578d4bd2","type":"api-call-service","z":"20b94110494c332a","name":"","server":"8be923df.d66f9","version":5,"debugenabled":false,"domain":"logbook","service":"log","areaId":[],"deviceId":[],"entityId":[],"data":"{\"name\":\"Test - HA Start Time\",\"message\":\"{{payload}}\",\"entity_id\":\"sensor.ha_uptime\",\"domain\":\"sensor\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":150,"y":280,"wires":[[]]},{"id":"8be923df.d66f9","type":"server","name":"homeassistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":""}]

That is great and I will use it but how do I get info like what flow triggered the event, say my lights turn on when I don’t want them to, I need to know what triggered that in Node red… like what flow tab or line…

And I get an error with the HA log
call service node is missing api “domain” property, not found in config or payload
Here is my info

    "name": "Test - HA Start Time",
    "message": "Light On",
    "entity_id": "light.living_room_lamp",
    "domain": "light"

I’m using the “sensor” domain in the Call Service Node, so maybe you don’t have that??

Here’s a simpler flow to turn a lamp on at 21:00 hours and manually log it to the logbook.

[{"id":"827a95c986eb80bd","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"9b0ac5840ade0fde","type":"api-call-service","z":"827a95c986eb80bd","name":"","server":"8be923df.d66f9","version":5,"debugenabled":false,"domain":"logbook","service":"log","areaId":[],"deviceId":[],"entityId":[],"data":"{\"name\":\"Node Red Logging\",\"message\":\"Lamp 1 turned on by my Node Red flow #1\",\"entity_id\":\"light.lamp_1_on_off\",\"domain\":\"light\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":390,"y":200,"wires":[[]]},{"id":"899f4234e9c68066","type":"inject","z":"827a95c986eb80bd","name":"Turn Light on at 20:00 hours","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 21 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":140,"wires":[["50815637d537c3c4"]]},{"id":"50815637d537c3c4","type":"api-call-service","z":"827a95c986eb80bd","name":"Turn Light On","server":"8be923df.d66f9","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.lamp_1_on_off"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":160,"y":200,"wires":[["9b0ac5840ade0fde"]]},{"id":"8be923df.d66f9","type":"server","name":"homeassistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":""}]

You should see 2 entries in the Logbook, one is the from the default logging in HA, and the other is from the flow in Node Red.

Fantastic thanks!!!

the 2nd one gives me a syntax error SyntaxError:
Expected ‘,’ or ‘}’ after property value in JSON at position 237
:"{\"name\""Test - HA St

btw, do you have to connect these nodes to each of your flows or does it pick up all the information on its’ own once it’s in a flow?

I add them into flows as needed - they won’t do anything unless you have something connected to them (input) to fire them.

For adding something to the log book, add a generic ‘Call Service’ node into a flow, open it up and select the domain “logbook”, the service “log”, and change the Data Type from J: expression to {} JSON. Scroll down and you’ll see a button named “Load example data”. Click that and it will populate the data field with an example like this:

    "name": "Kitchen",
    "message": "is being used",
    "domain": "light"


You can then edit the text to something you like, Add an Inject node in front of it and test it manually. You should see a new logbook entry like this: “Kitchen is being used triggered by service logbook.log”

1 Like