Node-red - My experience so far

Hi @Wabiloo, please see below

[
    {
        "id": "f73d565e.177398",
        "type": "inject",
        "z": "2c03c33235.b34fc",
        "name": "7PM trigger",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "00 20 * * *",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 280,
        "wires": [
            [
                "b3191444.5932a"
            ]
        ]
    },
    {
        "id": "b3191444.5932a",
        "type": "api-current-state",
        "z": "2c07c388.b434fc",
        "name": "",
        "server": "708d1e3a.ef397",
        "halt_if": "",
        "override_topic": true,
        "override_payload": true,
        "entity_id": "calendar.bins_schedule",
        "x": 410,
        "y": 280,
        "wires": [
            [
                "91064877.5df9e8"
            ]
        ]
    },
    {
        "id": "bb6efbc9.573dc8",
        "type": "function",
        "z": "2c07c388.b434fc",
        "name": "Day before bin day ?",
        "func": "var nextCollectionDate = msg.data.attributes.start_time;\nvar bin_date = new Date(nextCollectionDate);\nvar today = new Date();\nbin_date.setDate(bin_date.getDate()-1);\n\nif ((bin_date.getDate() === today.getDate()) && \n    (bin_date.getMonth() === today.getMonth()) && \n    (bin_date.getFullYear() === today.getFullYear()))\n    {\n      msg.payload = \"true\";\n      return msg; \n    }\n    else\n    {\n        return null;\n    }\n",
        "outputs": 1,
        "noerr": 0,
        "x": 680,
        "y": 320,
        "wires": [
            [
                "298edf5e.3baa6"
            ]
        ]
    },
    {
        "id": "91064877.5df9e8",
        "type": "change",
        "z": "2c07c388.b434fc",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "data.attributes.start_time",
                "pt": "msg",
                "from": "00:00:00",
                "fromt": "str",
                "to": "07:00:00",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 410,
        "y": 320,
        "wires": [
            [
                "bb6efbc9.573dc8"
            ]
        ]
    },
    {
        "id": "298edf5e.3baa6",
        "type": "template",
        "z": "2c07c388.b434fc",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{\n   \"data\":{\n      \"where\":\"kitchen\",\n      \"what\":\"Good evening. This is a reminder. Tommorow is a {{data.attributes.message}}.\"\n   }\n}",
        "output": "str",
        "x": 860,
        "y": 320,
        "wires": [
            [
                "ce693f4e.e74df"
            ]
        ]
    },
    {
        "id": "ce693f4e.e74df",
        "type": "api-call-service",
        "z": "2c07c388.b434fc",
        "name": "Sonos Say",
        "server": "708d1e3a.ef397",
        "service_domain": "script",
        "service": "sonos_tts",
        "data": "{}",
        "mergecontext": "",
        "x": 1010,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "708d1e3a.ef397",
        "type": "server",
        "z": "",
        "name": "Home Assistant",
        "legacy": false,
        "hassio": false,
        "rejectUnauthorizedCerts": true
    }
]

@Dilby

Can you provide a bit more detail on how you set up the trash collection notice?

Sure.

My local authority share an ical calendar that I can be added to my Google account. I then use the Google Calendar integration to pull in all of my calendars (you can filter which calendars are pulled in through the automatically created google_calendars.yaml file.

I the use node-red to read the sensor, The attribute data includes the date/time info along with which trash can is collected that week (Attribute data is created from the calendar entry, so depends on the local authority making sure it’s in the calendar details).
As I would like the notification that the collection is due the evening before, I have to manipulate the date attribute to do that comparison.
I have a trigger that fires at 7pm every day, then do a comparison… if (bin_collection_day - 1day) == today then send notification.
I’ll share that part of the flow this evening.

@dilby, I’ve got the following working, but in my never ending quest to simplify things (and learn), I would like to be able to accomplish this without having to duplicate everything. I’m unable to use Sonos Say, so I have it working with Alexa (which works better for this announcement), but not sure how to accomplish both trash and trash_recycle in the same “flow” since I had to change to template for “plain text” for the Alexa tts.

I’m using this Sonos-Node and it works flawless

For the calendar events I’m using the Google-Calendar-Node which is triggering an event for every calendar entry. The good think is that you can schedule different messages and types.

I schedule the calendar entry for the time I want the reminder sent out, so no need to check for ‘day-before’. Unfortunately, my council is not providing an online calendar for which I can find out what bin is getting emptied.

Its worth a try

My recycling and general trash are in the same calendar, so don’t have the same challenge to simplify.

Could you do a comparison of the start times to check which of the two is sooner, and pass that through ?

I have tried to keep as much of the integrations with other services and general state within home-assistant, leaving node-red to simply handle automations. Mainly because i keep some essential automations within home-assistant in the event node-red breaks or somehow looses the connection (although this has yet to happen).

I do not compare time.

  • Whatever event comes first is executing the announcement
  • I use a gate node (combined withe a trigger for 2days) which is blocking any further announcement for 2 days

@Dilby

Hi, firstly apologies for digging up an old thread. I love the bin reminder that you have set up and I am trying to replicate the same thing in my own Node Red flow. I am having a few problems and wondered if you might be able to point me in the right direction.

I have a Google Calendar provided by my local authority that looks pretty similar to what you are using. I have set up the Google Calendar integration in HA and I can see the sensors created. The sensor for the bin calendar never switches to on so the Node Red state detection never triggers. If I force it to change manually using the HA Dev Tools then it does change but the only output from the current_state node is on, the details of the next appointment that I am expecting to see in the payload are not there.

I would really appreciate any help that you are able to give me.

Thanks in advance.

Hi there,

I’m not currently in front of my flow so will give a general answer now (and something more useful later).

I’m not relying on the “on” state to trigger the flow. I have to use a trigger node to poll the sensor at a specific time each day. In my case at 7pm each day it will poll the sensor.
Secondly, the useful information is passed through as an attribute rather than as a part of the state.

[Edit]Just realised, i’m not using a sensor in my flow. I’m simply using ‘calendar’ state that is reporting an attribute with the next bin collection. For this use-case, you shouldn’t need any sensors unless you want to fire an event at the exact time of the calendar event.

Sorry, I made a mistake in my initial description and I have done some more troubleshooting.

I am actually using a ‘current_state’ node to query calendar.bins_schedule. This always shows a status of off but I have now found that it passes the details of the next calendar entry in the output regardless. I have checked with a debug node and the state node is outputting:

{"_msgid":"9100367.84279c8",
"topic":"calendar.bins_schedule",
"payload":"off",
"data":
  {"entity_id":"calendar.bins_schedule",
  "state":"off",
  "attributes":
   {"message":"Black Bin Collection",
    "all_day":true,
   "start_time":"2020-02-13 00:00:00",
    "end_time":"2020-02-14 00:00:00",
    "location":"",
  "description":"",
    "offset_reached":false,
    "friendly_name":"Bins Schedule"},
    "last_changed":"2020-02-12T11:07:45.007477+00:00",
    "last_updated":"2020-02-12T11:07:45.007477+00:00",
  "context":
   {"id":"d9861d6fda6f4ccf84044a93b86ce92e",
    "parent_id":null,
    "user_id":null},
  "timeSinceChangedMs":2165918}}

I have followed this through the flow and everything is working as expected (start time is changed etc.) but when it gets to the template to format the data for TTS I get an “Error get-template: Request failed with status code 400”. The content of my template is:

{
    "data": {
        "target": "Study Echo",
        "message": “Don't forget that tomorrow is a {{data.attributes.message}}.”,
        "data": {
            "type": "announce"
        }
    }
}

I’m so close to getting this working…

I figured it out. I was using the ‘get template’ node from node-red-contrib-home-assistant-websocket, I switched to the standard Node-Red template and it worked!!

Thanks for taking a look and nudging me towards a solution :slight_smile:

Glad you got it working :).

I Dilby, sorry to be a pain, I am new to this coming from Vera and I’ve been struggling to get my morning weather report working. Do you mind sharing the node red export you have above for your darksky flow? I can get my Sonos to say one summary, but I can’t get it to work with joining mulitple ones. I’m clearly missing something and found your screenshot which is pointing me in the right direction.

Many thanks

Sure thing.

I’ve put the snippet here : weather.json

Awesome thank you so much!

Would you be able to share the code of your flow. I have a similar calendar from the city.

[{"id":"81557b8aadc476ea","type":"tab","label":"Clipboard","disabled":false,"info":""},{"id":"0e02443610527534","type":"group","z":"81557b8aadc476ea","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["937d5dec718c44cb","c977b09c7b910438","6efa1660ac05cef0","a4d009ac31cd4f36","b9ae28ee321e1995","5a5f85e5fc01bc65"],"x":31,"y":118,"w":692,"h":162},{"id":"937d5dec718c44cb","type":"comment","z":"81557b8aadc476ea","g":"0e02443610527534","name":"Trash Day Announce","info":"","x":157,"y":159,"wires":[]},{"id":"c977b09c7b910438","type":"api-current-state","z":"81557b8aadc476ea","g":"0e02443610527534","name":"Trash Day","server":"df79addc.b2967","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.trash_recycle","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":307,"y":199,"wires":[["6efa1660ac05cef0"],[]]},{"id":"6efa1660ac05cef0","type":"template","z":"81557b8aadc476ea","g":"0e02443610527534","name":"TTS","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"Good evening. This is a reminder. Tommorow is trash and recycle day.","output":"str","x":467,"y":199,"wires":[["a4d009ac31cd4f36"]]},{"id":"a4d009ac31cd4f36","type":"api-call-service","z":"81557b8aadc476ea","g":"0e02443610527534","name":"Alexa TTS","server":"df79addc.b2967","version":5,"debugenabled":false,"domain":"notify","service":"alexa_media","areaId":[],"deviceId":[],"entityId":[],"data":"{\"message\":\"{{payload}}\",\"data\":{\"type\":\"tts\",\"method\":\"speak\"},\"target\":[\"media_player.echo_show\"]}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"data"}],"queue":"none","x":627,"y":199,"wires":[[]]},{"id":"b9ae28ee321e1995","type":"inject","z":"81557b8aadc476ea","g":"0e02443610527534","name":"test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":127,"y":199,"wires":[["c977b09c7b910438"]]},{"id":"5a5f85e5fc01bc65","type":"inject","z":"81557b8aadc476ea","g":"0e02443610527534","name":"10PM","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 22 * * 3,4","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":137,"y":239,"wires":[["c977b09c7b910438"]]},{"id":"df79addc.b2967","type":"server","name":"HomeAssistant","version":4,"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"}]

Thank You. My flow was fairly simple just turning on a light based on the type of collection that is blue for recycling etc