Extract Last BackUp Date from Samba BackUp sensor (HA)?

Hello fellow NodeRed users,

I'm a begginer, so please genltle :slight_smile:
I try to create an alerting system if the BackUp of my HomeAssistant server was not done on time. I have I nightly back-up done via the Samba Back-Up AddOn. This addon creates an sensor which outputs different attributes. One of them is the last_backup. I was trying to compare this date to yesterday in a switch node using JSONata $moment().subtract(1,"days"), but I realized that the last_backup data is not actually in a date format.

Could you please help me out to convert the last_backup info into a date so that I can calculate the differnce to the current date?

Or any other sugesstion is wellcomed!

Thanks and wish you all Merry Christmas!

A unix timestamp in milliseconds

$moment("2022-12-22 00:00").diff($moment().subtract(1,"days"))

A formatted differnce

$moment(
   $moment("2022-12-22 00:00").diff($moment().subtract(1,"days"))
).format("H:mm")

$moment() will convert your string to a moment as is, you can get the difference also Moment.js | Docs

Or just the iISO output convertion

$moment("2022-12-22 00:00").format()
1 Like

Thank you Sir! You have led me to the solution.

I have used: $moment(payload).diff($moment(),"days")

I belive it's messy and could be done everthing in one node, but it works and displays the backup age in Lovelace dashboard (soon will trigger a warning light)

image

If somebody is loking for the same solution, please find the code bellow.

[{"id":"85194148c5dfda7b","type":"api-current-state","z":"1cad30817e0e6c90","name":"Samba","server":"7ccbf84f.b88258","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.samba_backup","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":360,"y":4340,"wires":[["fe389d8cd63c524d"]]},{"id":"fae578a52684f127","type":"inject","z":"1cad30817e0e6c90","name":"TimeStamp","props":[{"p":"payload"}],"repeat":"","crontab":"00 02 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":4340,"wires":[["85194148c5dfda7b"]]},{"id":"fe389d8cd63c524d","type":"change","z":"1cad30817e0e6c90","name":"LastBackUpAtribute->payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"data.attributes.last_backup","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":4340,"wires":[["50ead193073310a1"]]},{"id":"50ead193073310a1","type":"change","z":"1cad30817e0e6c90","name":"Days from last backup","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload).diff($moment(),\"days\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":4340,"wires":[["c9d05aae8a86e35d"]]},{"id":"c9d05aae8a86e35d","type":"api-call-service","z":"1cad30817e0e6c90","name":"SetBackupAge","server":"7ccbf84f.b88258","version":5,"debugenabled":false,"domain":"input_number","service":"set_value","areaId":[],"deviceId":[],"entityId":["input_number.backupage"],"data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1120,"y":4340,"wires":[[]]},{"id":"7ccbf84f.b88258","type":"server","name":"HomeAssistantOK","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.