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

**URL:** <https://discourse.nodered.org/t/extract-last-backup-date-from-samba-backup-sensor-ha/72604>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [22 December 2022 20:56 UTC](https://discourse.nodered.org/t/extract-last-backup-date-from-samba-backup-sensor-ha/72604 "2022-12-22T20:56:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BernardS](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@BernardS](https://discourse.nodered.org/u/BernardS)\
**Post date:** [22 December 2022 20:56 UTC](https://discourse.nodered.org/t/extract-last-backup-date-from-samba-backup-sensor-ha/72604/1 "2022-12-22T20:56:33Z")

</div>

Hello fellow NodeRed users,

I'm a begginer, so please genltle 🙂  
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!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/24aa7a24958e637875ee52df488a9203401d94ba.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 December 2022 22:33 UTC](https://discourse.nodered.org/t/extract-last-backup-date-from-samba-backup-sensor-ha/72604/2 "2022-12-22T22:33:28Z")

</div>

A unix timestamp in milliseconds

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

```

A formatted differnce

```auto
$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](https://momentjs.com/docs/#/displaying/difference/)

Or just the iISO output convertion

```auto
$moment("2022-12-22 00:00").format()

```

---

<div class="post-metadata">

**Author:** ![BernardS](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@BernardS](https://discourse.nodered.org/u/BernardS)\
**Post date:** [23 December 2022 19:14 UTC](https://discourse.nodered.org/t/extract-last-backup-date-from-samba-backup-sensor-ha/72604/3 "2022-12-23T19:14:58Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/89fd109f490ca6d6b9fee609409864b3b84fb10c.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a98b6816bd67b5ac9175c8b9232dbffe3531ed1f.png)

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

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 January 2023 19:15 UTC](https://discourse.nodered.org/t/extract-last-backup-date-from-samba-backup-sensor-ha/72604/4 "2023-01-06T19:15:30Z")

</div>

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