{"id":13287,"date":"2021-03-22T12:55:09","date_gmt":"2021-03-22T12:55:09","guid":{"rendered":"https:\/\/www.rjt.org.uk\/home\/?post_type=home_assistant_tip&#038;p=13287"},"modified":"2021-03-24T09:47:33","modified_gmt":"2021-03-24T09:47:33","slug":"create-syncback-sensors","status":"publish","type":"home_assistant_tip","link":"https:\/\/www.rjt.org.uk\/home\/archives\/home-assistant-tip\/create-syncback-sensors\/","title":{"rendered":"Create Syncback Sensors"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"588\" height=\"658\" src=\"https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/03\/2021-03-23_091442.png\" alt=\"\" class=\"wp-image-13289\" srcset=\"https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/03\/2021-03-23_091442.png 588w, https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/03\/2021-03-23_091442-268x300.png 268w, https:\/\/www.rjt.org.uk\/home\/wp-content\/uploads\/2021\/03\/2021-03-23_091442-134x150.png 134w\" sizes=\"auto, (max-width: 588px) 100vw, 588px\" \/><\/figure><\/div>\n\n\n\n<p><br>The following script can be added to Syncback Pro to set a sensor for each Profile when the backup completes each will be prefixed with sensor.syncback_, when attached to a profile the script will create or update a sensor for the backup selected.<\/p>\n\n\n\n<p>The friendly name will be set to the Profile name and the Result set to the result of the run.<\/p>\n\n\n\n<p>To use create Long access Token in your Home Assistant Profile and replace TOKEN in the script with your token.<\/p>\n\n\n\n<p>Note:  The datetime field in the attributes is to ensure the Last Updated time on the entity is updated.<\/p>\n\n\n\n<p>The Json string sent to Home assistant looks like this<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\"entity_id\": \"sensor.syncback_home_assistant\", \"state\": \"ok\", \"attributes\": {\"result\": \"Success\", \"friendly_name\": \"Home Assistant\", \"area\": \"syncback\", \"count\": \"9\", \"datetime\": \"Wed 24 03 2021 09:08:08\"}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Syncback Pro Basic Script<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">'\n' Send Result of run to Home Assistant\n' after a profile runs (if the run is simulated then it does not).\n'\n' SBLang=Basic\n'\n'\n\n' The connection details for Home Assistant to send a message\nconst HAToken = \"TOKEN\"\nconst HAEntity = \"sensor.syncback\"\n\n'\n' This is a runtime script\n'\nFunction Description(ByRef ScriptType)\n  Description = \"Send Home Assistant Update\"\n  ScriptType = SCRIPTTYPE_RUN\nEnd Function\n\n'\n' This routine is called after the profile has finished.\n'\nSub RunProfileResult(ProfileResult, ErrMsg)\n  Dim sUrl\n  Dim sText\n  Dim sJson\n  Dim oXMLHTTP, sResult\n  Dim SendHA\n  Dim sAuth\n  Dim sAttribute\n  Dim sStatus\n  Dim sDesc\n\n  ' Check to see if the profile has run\n  If SBRunning.Simulated then\n    Exit Sub\n  End If\n\n  ' Get profile result\n  SendHA = True\n  Select Case ProfileResult\n  Case 0\n    ' 0 = No result (ELR_None)\n    SendHA = False\n  Case 1\n    ' 1 = Unknown profile (ELR_UnknownProfile)\n    SendHA = False\n  Case 2\n    ' 2 = Profile is already running (ELR_AlreadyRunning)\n    SendHA = False\n  Case 3\n    ' 3 = Profile has been imported and not run yet (ELR_Imported)\n    SendHA = False\n  Case 4\n    ' 4 = Profile is running (ELR_Running)\n    SendHA = False\n  End Select\n\n  ' Are we going to update HA?\n  If SendHA = False then\n    Exit Sub\n  End If\n\n  sStatus = \"error\"\n  ' Build the Home Assistant message\n  sDesc = \"Profile \" &amp; SBRunning.Name &amp; \" finished on \" &amp; FormatDateTime(Now(), 0) &amp; \":\"\n  sText = \"\"\n  Select Case ProfileResult\n  Case 5\n    sText = sText &amp; \"Internal error\"\n  Case 9\n    sText = sText &amp; \"Restore aborted\"\n  Case 10\n    sText = sText &amp; \"Restore failed\"\n  Case 11\n    sText = sText &amp; \"Restore success\"\n  Case 12\n    sText = sText &amp; \"Aborted\"\n  Case 13\n    sText = sText &amp; \"Failed\"\n  Case 14\n    sText = sText &amp; \"Success\"\n    sStatus = \"ok\"\n  Case 15\n    sText = sText &amp; \"Network failure\"\n  Case 16\n    sText = sText &amp; \"Scan failure\"\n  Case 17\n    sText = sText &amp; \"Compare failure\"\n  Case 18\n    sText = sText &amp; \"Run Before stopped profile\"\n  Case 19\n    sText = sText &amp; \"Profile disabled\"\n  Case 20\n    sText = sText &amp; \"SMART drive errors\"\n  Case 21\n    sText = sText &amp; \"Could not email log\"\n  Case 22\n    sText = sText &amp; \"Volume Shadow Copy snapshot failed\"\n  End Select\n\n\n  sAttribute = Lcase(replace(SBRunning.Name,\" \",\"_\"))\n  sAttribute = replace(sAttribute,\".\",\"_\")\n  sAttribute = replace(sAttribute,\"-\",\"_\")\n  dim sCount\n  dim sDT\n\n  sDT = DateToStr(Date) &amp; \" \" &amp; TimeToStr(Time)\n  sCount = SBRunning.GetRuntimeValueStr(1)\n  sJson =  \"{'state': '\"  &amp; sStatus &amp; \"', 'attributes': {'result': '\" &amp; sText &amp; \"'\"\n  sJson = sJson &amp;  \",'friendly_name': '\"&amp;  SBRunning.Name &amp; \"'\"\n  sJson = sJson &amp;  \",'area': 'syncback'\"\n  sJson = sJson &amp;  \",'count': '\" &amp; sCount &amp; \"'\"\n  sJson = sJson &amp;  \",'datetime': '\" &amp; sDT &amp; \"'\"\n  sJson = sJson &amp; \"}}\"\n  sJson = replace(sJson,\"'\",\"\"\"\")\n  SBRunning.DebugOut(\"HAResult\", sJson, 1)\n\n  ' Send HA message\n  sUrl = \"http:\/\/homeassistant.local:8123\/api\/states\/\" &amp; HAEntity &amp; \"_\" &amp; sAttribute\n  sAuth = \"Bearer \" &amp; HAToken\n  set oXMLHTTP = CreateObject(\"MSXML2.ServerXMLHTTP.6.0\")\n  oXMLHTTP.open(\"POST\", sUrl, false)\n  oXMLHTTP.SetRequestHeader(\"Content-Type\", \"application\/json\")\n  oXMLHTTP.SetRequestHeader(\"Authorization\", sAuth)\n\n  oXMLHTTP.send(sJson)\n  sResult = oXMLHTTP.responseText\n  SBRunning.DebugOut(\"HAResult\", sResult, 1)\n  Set oXMLHTTP = nothing\nEnd Sub<\/pre>\n\n\n\n<p>To display the backup information the follow was used,&nbsp; this includes the custom:flex-table-card from <a href=\"https:\/\/github.com\/custom-cards\/flex-table-card\">Custom cards for Home Assistant<\/a> installed via HACS.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">      - type: vertical-stack\n        cards:\n          - type: markdown\n            content: '# Backups'\n          - type: horizontal-stack\n            cards:\n              - type: markdown\n                content: >-\n                  **Last Snapshot**\n\n                  {{ as_timestamp(state_attr('sensor.snapshot_backup',\n                  'last_snapshot')) | timestamp_custom('%A at %H:%M \n                  (%d-%m-%y)') }}\n              - type: markdown\n                content: >-\n                  **Syncback**\n\n                  {%set errors =  states.sensor|\n                  selectattr('state','eq','error') |\n                  selectattr('attributes.area','eq','syncback')           \n                  |map(attribute='entity_id')|list |count  %}{%set good = \n                  states.sensor| selectattr('state','eq','ok') |\n                  selectattr('attributes.area','eq','syncback')           \n                  |map(attribute='entity_id')|list |count  %}{% if errors > 0\n                  %}&lt;font color=\"red\">{{errors}} Backup{% if errors > 1 %}s{%\n                  endif %} error&lt;\/font>\n\n                  {%endif%}{{good}} Good backup{% if not(good == 1) %}s{% endif\n                  %} \n          - type: 'custom:flex-table-card'\n            clickable: true\n            sort: last_updated\n            entities:\n              include: sensor.sync*\n            columns:\n              - name: Profile\n                data: friendly_name\n              - name: Result\n                data: result\n                modify: >-\n                    const color = (x == \"Success\") ? 'green' : 'red';\n                    '&lt;span style=\"color:' + color + ';\">' + x + '&lt;\/span>'\n\n              - name: Age (h)\n                data: last_updated\n                modify: Math.round((Date.now() - Date.parse(x)) \/ 36000.) \/ 100.<\/pre>\n","protected":false},"template":"","class_list":["post-13287","home_assistant_tip","type-home_assistant_tip","status-publish","hentry","comments-off"],"_links":{"self":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip\/13287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip"}],"about":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/types\/home_assistant_tip"}],"wp:attachment":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/media?parent=13287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}