Control Home Assistant Light Entity with MQTT using Node-RED

How to control a Home Assistant Light-entity via MQTT using Node-RED. Been a bit of a struggle, but this is how I did it. If you have any comments, please leave these below.

Prerequisites

  • Home Assistant (installed on Raspberry PI)
  • Node-RED installed on Hassio
  • MQTT-broker: Mosquitto installed on Hassio
  • Light-entity: a IKEA-light connected via Zigbee using deCONZ with a Conbee II-USB-stick

This is how my light-entity was configured in Home Assistant. Screen dump from Developer Tools in HA.

I choose to use MQTT to control the light because I wanted to control the light with an Arudrino with a LCD-display. This will be a future blogpost of how I did this…

With MQTT-box I published a MQTT-message to control the light

The Node-RED-flow to capture the MQTT-message and to respond to this looks like this:

[{"id":"dc2487082661e486","type":"tab","label":"MQTT-to-Services","disabled":false,"info":"","env":[]},{"id":"7651fd1fb0c45f22","type":"mqtt in","z":"dc2487082661e486","name":"ListenToLights","topic":"lights/#","qos":"0","datatype":"auto","broker":"a4eb3cbb92b65a6e","nl":false,"rap":true,"rh":0,"inputs":0,"x":110,"y":60,"wires":[["e4f1766bc8059118"]]},{"id":"deadd0816ccca8ed","type":"debug","z":"dc2487082661e486","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":200,"wires":[]},{"id":"e4f1766bc8059118","type":"json","z":"dc2487082661e486","name":"Format","property":"payload","action":"","pretty":true,"x":300,"y":60,"wires":[["b68b7b3daae18796","0cdffd5221664eeb"]]},{"id":"b68b7b3daae18796","type":"switch","z":"dc2487082661e486","name":"","property":"payload.service","propertyType":"msg","rules":[{"t":"eq","v":"turn_off","vt":"str"},{"t":"eq","v":"turn_on","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":190,"y":160,"wires":[["3b3ec72814baafc8"],["1ef43b08e3c0a2c7"]]},{"id":"1ef43b08e3c0a2c7","type":"api-call-service","z":"dc2487082661e486","name":"CallService TurnOn","server":"9e6f0ac5.0c6dc8","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"{{payload.entity_id}}","data":"{\"brightness\":payload.brightness, \"rgb_color\":[payload.red,payload.green,payload.blue]}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":370,"y":240,"wires":[["deadd0816ccca8ed"]]},{"id":"3b3ec72814baafc8","type":"api-call-service","z":"dc2487082661e486","name":"TurnOff","server":"9e6f0ac5.0c6dc8","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"{{payload.entity_id}}","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":400,"y":160,"wires":[[]]},{"id":"0cdffd5221664eeb","type":"debug","z":"dc2487082661e486","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":60,"wires":[]},{"id":"a4eb3cbb92b65a6e","type":"mqtt-broker","name":"Hssss","broker":"10.22.34.100","port":"1883","clientid":"ListenToLights","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"9e6f0ac5.0c6dc8","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

Two things which I struggled with was:

  1. The Switch-node. How to make Node-RED switch between ”turn_on” and ”turn_off” depending on the ”service”-information in the JSON-object. How I did it – screen shot below:
  2. I was having trouble with Node-RED beeing disconnected to the Mosquitto-broker. I solved this by creating a separate MQTT-server-configuration for each Node-RED-flow using with a different Client-ID.
The Switch-node configuration
The Turn-on-node
The expression field from the Turn-on-node. This is how you read the information from the msg-object

This is how my msg-object looked like after the JSON-node (converting the MQTT-string to a JSON-object).

This is how my node-red message looks like

A MQTT-message to turn on (or change) the light:

{"entity_id": "light.discolampa", "service":"turn_on", "red":0, "green":70, "blue": 90, "brightness":90}

A MQTT-message to turn off the ligth:

{"entity_id": "light.discolampa", "service":"turn_off"}

Hope this could help someone!