Sharing my learnings using "merge context" - Node-red and cron job based on solar events

Hi,
2 days of learning - just sharing some findings if it helps.

Using the “merge context” for the parameters of a service call
Using a mix of cron types to create my own “part of day”

  • modify an input_text in HA for different times of the day (to be used in automations etc)
  • Mix a dynamic cron based on latitude to figure out when it’s dark and light outside
  • Use standard cron for other options at specific times
  • use a bank holiday node to delay when my “morning starts”
[{"id":"6f3933f2.363d4c","type":"switch","z":"4e7dfe47.14c27","name":"Sun Phases","property":"payload.status.solarState.state","propertyType":"msg","rules":[{"t":"eq","v":"nauticalDawn","vt":"str"},{"t":"eq","v":"nauticalDusk","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":300,"wires":[["65900152.90ffc"],["919c61fa.86707"]]},{"id":"bfd23cf2.ed4a1","type":"comment","z":"4e7dfe47.14c27","name":"Cron-Plus: Description of all sunphases","info":"sunrise\t\n  sunrise (top edge of the sun appears on the horizon)\n  \nsunriseEnd\t\n  sunrise ends (bottom edge of the sun touches the horizon)\n  \ngoldenHourEnd\t\n  morning golden hour (soft light, best time for photography) ends\n  \nsolarNoon\t\n  solar noon (sun is in the highest position)\n  \ngoldenHour\t\n  evening golden hour starts\n  \nsunsetStart\t\n  sunset starts (bottom edge of the sun touches the horizon)\n  \nsunset\t\n  sunset (sun disappears below the horizon, evening civil twilight starts)\n  \ndusk\t\n  dusk (evening nautical twilight starts)\n  \nnauticalDusk\t\n  nautical dusk (evening astronomical twilight starts)\n  \nnight\t\n  night starts (dark enough for astronomical observations)\n  \nnadir\t\n  nadir (darkest moment of the night, sun is in the lowest position)\n  \nnightEnd\t\n  night ends (morning astronomical twilight starts)\n  \nnauticalDawn\t\n  nautical dawn (morning nautical twilight starts)\n  \ndawn\t\n  dawn (morning nautical twilight ends, morning civil twilight starts)\n","x":270,"y":360,"wires":[]},{"id":"65900152.90ffc","type":"change","z":"4e7dfe47.14c27","name":"Light","rules":[{"t":"set","p":"mydaytime","pt":"global","to":"{\"value\": \"light\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":280,"wires":[["c475756b.9f2838"]]},{"id":"2bb03c79.a99074","type":"cron","z":"4e7dfe47.14c27","name":"7am","cronExpression":"0 7 * * *","fireAtStartup":false,"x":170,"y":120,"wires":[["97c38f48.19ddf"]]},{"id":"97c38f48.19ddf","type":"holiday","z":"4e7dfe47.14c27","name":"Bank Holiday","country":"NL","state":"","region":"","languages":"","typePublic":true,"typeBank":true,"typeSchool":false,"typeObservance":false,"typeOptional":false,"weekend":"6,0","x":330,"y":120,"wires":[["7793f3de.d10bcc"],["f0f859f8.8a9e68"]]},{"id":"7793f3de.d10bcc","type":"delay","z":"4e7dfe47.14c27","name":"3h later","pauseType":"delay","timeout":"3","timeoutUnits":"hours","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":100,"wires":[["f0f859f8.8a9e68"]]},{"id":"10e9d1ac.1b918e","type":"cron","z":"4e7dfe47.14c27","name":"Noon","cronExpression":"0 12 * * *","fireAtStartup":false,"x":170,"y":160,"wires":[["915eea7a.0257e8"]]},{"id":"f757c21a.8194c","type":"cron","z":"4e7dfe47.14c27","name":"18h","cronExpression":"0 18 * * *","fireAtStartup":false,"x":170,"y":200,"wires":[["adfead17.95b58"]]},{"id":"6ae7326a.d201ac","type":"cron","z":"4e7dfe47.14c27","name":"23h30 - Night","cronExpression":"30 23 * * *","fireAtStartup":false,"x":190,"y":240,"wires":[["b7c305fe.93e448"]]},{"id":"c475756b.9f2838","type":"api-call-service","z":"4e7dfe47.14c27","name":"","server":"a0eea9da.0fe1d8","version":1,"debugenabled":true,"service_domain":"input_text","service":"set_value","entityId":"input_text.my_day_phase","data":"","dataType":"json","mergecontext":"mydaytime","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":870,"y":300,"wires":[[]]},{"id":"f0f859f8.8a9e68","type":"change","z":"4e7dfe47.14c27","name":"Morning","rules":[{"t":"set","p":"mydaytime","pt":"global","to":"{\"value\": \"morning\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":120,"wires":[["c475756b.9f2838"]]},{"id":"b7c305fe.93e448","type":"change","z":"4e7dfe47.14c27","name":"Night","rules":[{"t":"set","p":"mydaytime","pt":"global","to":"{\"value\": \"night\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":240,"wires":[["c475756b.9f2838"]]},{"id":"919c61fa.86707","type":"change","z":"4e7dfe47.14c27","name":"Dark","rules":[{"t":"set","p":"mydaytime","pt":"global","to":"{\"value\": \"dark\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":320,"wires":[["c475756b.9f2838"]]},{"id":"adfead17.95b58","type":"change","z":"4e7dfe47.14c27","name":"Evening","rules":[{"t":"set","p":"mydaytime","pt":"global","to":"{\"value\": \"evening\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":200,"wires":[["c475756b.9f2838"]]},{"id":"915eea7a.0257e8","type":"change","z":"4e7dfe47.14c27","name":"Afternoon","rules":[{"t":"set","p":"mydaytime","pt":"global","to":"{\"value\": \"afternoon\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":160,"wires":[["c475756b.9f2838"]]},{"id":"3cec4d3a.fb0c62","type":"cronplus","z":"4e7dfe47.14c27","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"schedule1","topic":"schedule1","payloadType":"default","payload":"","expressionType":"solar","expression":"0 * * * * * *","location":"51.889462622429704 4.462959221564233","offset":"0","solarType":"selected","solarEvents":"nightEnd,nauticalDusk"}],"x":180,"y":300,"wires":[["6f3933f2.363d4c"]]},{"id":"a0eea9da.0fe1d8","type":"server","name":"Home Assistant New","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
  • You’ll need the following palette
  • node-red-contrib-cron-plus
  • node-red-contrib-holiday
  • and of course the home assistant node-red module

Learnings about using the “merge context”

Setting a parameter
In the node, function, etc

  • select “global” where it normally says “msg” - and give it a name
  • under, change the selector to JSON and setup your parameters
  • ex: {“value”: “morning”} or {“brightness_pct”: “50”}
  • tip: if you don’t like to code (like me) you can use the three dots … on the right and chose the tab “visual editor”. Then under the arrow, add item. The first part is your attribute, the second is your parameter.
  • You can add more attributes to pass on: example
    {"brightness_pct":"50","color_temp":"337"}

Reusing this parameter
In the service node of HA, simply fill the field “merge context” with the name you gave above.

Examples of this use?
Automations on multiple thermostats. You can define the “temperature” in a central node and create your “merge context” fields only one. Then the climate automations will call the temparature base on that master list.
Your pet turns on the sensors at night? Set different brightness for different times centrally and then apply it to your automations triggering your sensors.
You have lights outside? Have their brightness change based on the moment it gets dark, and even dim them when the sun starts showing up.

4 Likes