Setting up Dimmer in Node Red with Hue switch/dimmer

Hello,

I wanted to share my design for creating an on/off and dimmer function in Node Red.

this is the Flow I have created

here is the explanation:

image
it all starts with the butten, just plain, only put the butten input here, no validation or any of that.

next, the switch node, were we filter out the different button presses:
image
For every type of press you create a channel:


these will be the different fuctions:

  • on_press: light will go on
  • up_press: brightness will go up
  • down_press: brightness will go down
  • off_press: light will go out

turning the light on or off is nothing special, as you can see here:


to turn the light off, just put turn_off in the service field.

then, we move to the more interesting part, first I want to know if the light is on or not.
if the light is on, I would need to fetch the current brightness and for mopr brightness I would increase it with the amount I would like to, I have chosen for steps of 20.


you will need to add a new msg and give it a name, like mine is “brightness”, then change the type to JSON and fill in: $entity().attributes.brightness this will fetch the current brightness value for you, however, we want to change it up or down, so what we can do is that we add + or subtract - a value. I have used $entity().attributes.brightness+20 in my example and $entity().attributes.brightness-20 for lowering the brightness.

both top outputs are routed to a single call service node
image
From both currect state nodes we are filling the msg.brightness message, as we are looking into that single message, one node will suffice to so both, increase and decrease the brightness of your lamp.

this is just the same as turning the lamp on, however the important part is that we fill in the Data field with the following information:
{“brightness”:“{{brightness}}”}
the first brightness is referring to the property we can sent to the lamp.
the second brightness is the value coming from msg.brightness that we just got passed from the current state node, that contains the new to set brightness value.

this was it,
of course you can make tweaks to it, like use the brightness_pct, but then you would need to change the integer to a percentage, let me know what you think about this one. you would also be able to create this with a single switch that supports atleast 4 different clicks (like the aqara; single, double, triple, and quad)


> [
>     {
>         "id": "8050f576488419f2",
>         "type": "tab",
>         "label": "test flows",
>         "disabled": false,
>         "info": "",
>         "env": []
>     },
>     {
>         "id": "4118cf0cb723bd54",
>         "type": "server-state-changed",
>         "z": "8050f576488419f2",
>         "name": "Hue Button",
>         "server": "f6a3415e.59246",
>         "version": 5,
>         "outputs": 1,
>         "exposeAsEntityConfig": "",
>         "entityId": "sensor.philips_dimmer_action",
>         "entityIdType": "exact",
>         "outputInitially": false,
>         "stateType": "str",
>         "ifState": "",
>         "ifStateType": "str",
>         "ifStateOperator": "is",
>         "outputOnlyOnStateChange": true,
>         "for": "0",
>         "forType": "num",
>         "forUnits": "minutes",
>         "ignorePrevStateNull": false,
>         "ignorePrevStateUnknown": false,
>         "ignorePrevStateUnavailable": false,
>         "ignoreCurrentStateUnknown": false,
>         "ignoreCurrentStateUnavailable": false,
>         "outputProperties": [
>             {
>                 "property": "payload",
>                 "propertyType": "msg",
>                 "value": "",
>                 "valueType": "entityState"
>             },
>             {
>                 "property": "data",
>                 "propertyType": "msg",
>                 "value": "",
>                 "valueType": "eventData"
>             },
>             {
>                 "property": "topic",
>                 "propertyType": "msg",
>                 "value": "",
>                 "valueType": "triggerId"
>             }
>         ],
>         "x": 320,
>         "y": 320,
>         "wires": [
>             [
>                 "4d199824eca8c556"
>             ]
>         ]
>     },
>     {
>         "id": "4d199824eca8c556",
>         "type": "switch",
>         "z": "8050f576488419f2",
>         "name": "",
>         "property": "payload",
>         "propertyType": "msg",
>         "rules": [
>             {
>                 "t": "eq",
>                 "v": "on_press",
>                 "vt": "str"
>             },
>             {
>                 "t": "eq",
>                 "v": "up_press",
>                 "vt": "str"
>             },
>             {
>                 "t": "eq",
>                 "v": "down_press",
>                 "vt": "str"
>             },
>             {
>                 "t": "eq",
>                 "v": "off_press",
>                 "vt": "str"
>             }
>         ],
>         "checkall": "true",
>         "repair": false,
>         "outputs": 4,
>         "x": 490,
>         "y": 320,
>         "wires": [
>             [
>                 "d463aa089066fd28"
>             ],
>             [
>                 "d6464ebf67ce3b53"
>             ],
>             [
>                 "be094d66f2abae65"
>             ],
>             [
>                 "c819a54ce4380ea7"
>             ]
>         ]
>     },
>     {
>         "id": "d463aa089066fd28",
>         "type": "api-call-service",
>         "z": "8050f576488419f2",
>         "name": "",
>         "server": "f6a3415e.59246",
>         "version": 5,
>         "debugenabled": false,
>         "domain": "light",
>         "service": "turn_on",
>         "areaId": [],
>         "deviceId": [
>             "4a5d33b455c5d987e1e127740efe4dfe"
>         ],
>         "entityId": [],
>         "data": "",
>         "dataType": "jsonata",
>         "mergeContext": "",
>         "mustacheAltTags": false,
>         "outputProperties": [],
>         "queue": "none",
>         "x": 830,
>         "y": 220,
>         "wires": [
>             []
>         ]
>     },
>     {
>         "id": "c819a54ce4380ea7",
>         "type": "api-call-service",
>         "z": "8050f576488419f2",
>         "name": "",
>         "server": "f6a3415e.59246",
>         "version": 5,
>         "debugenabled": false,
>         "domain": "light",
>         "service": "turn_off",
>         "areaId": [],
>         "deviceId": [
>             "4a5d33b455c5d987e1e127740efe4dfe"
>         ],
>         "entityId": [],
>         "data": "",
>         "dataType": "jsonata",
>         "mergeContext": "",
>         "mustacheAltTags": false,
>         "outputProperties": [],
>         "queue": "none",
>         "x": 830,
>         "y": 400,
>         "wires": [
>             []
>         ]
>     },
>     {
>         "id": "168d5252b24528fa",
>         "type": "api-call-service",
>         "z": "8050f576488419f2",
>         "name": "Turn brightness up/down",
>         "server": "f6a3415e.59246",
>         "version": 5,
>         "debugenabled": true,
>         "domain": "light",
>         "service": "turn_on",
>         "areaId": [],
>         "deviceId": [
>             "4a5d33b455c5d987e1e127740efe4dfe"
>         ],
>         "entityId": [],
>         "data": "{\"brightness\":\"{{brightness}}\"} ",
>         "dataType": "json",
>         "mergeContext": "",
>         "mustacheAltTags": false,
>         "outputProperties": [],
>         "queue": "none",
>         "x": 1070,
>         "y": 300,
>         "wires": [
>             []
>         ]
>     },
>     {
>         "id": "be094d66f2abae65",
>         "type": "api-current-state",
>         "z": "8050f576488419f2",
>         "name": "Brightness -",
>         "server": "f6a3415e.59246",
>         "version": 3,
>         "outputs": 2,
>         "halt_if": "on",
>         "halt_if_type": "str",
>         "halt_if_compare": "is",
>         "entity_id": "light.lamp_filament_test",
>         "state_type": "str",
>         "blockInputOverrides": false,
>         "outputProperties": [
>             {
>                 "property": "payload",
>                 "propertyType": "msg",
>                 "value": "",
>                 "valueType": "entityState"
>             },
>             {
>                 "property": "data",
>                 "propertyType": "msg",
>                 "value": "",
>                 "valueType": "entity"
>             },
>             {
>                 "property": "brightness",
>                 "propertyType": "msg",
>                 "value": "$entity().attributes.brightness-20",
>                 "valueType": "jsonata"
>             }
>         ],
>         "for": "0",
>         "forType": "num",
>         "forUnits": "minutes",
>         "override_topic": false,
>         "state_location": "payload",
>         "override_payload": "msg",
>         "entity_location": "data",
>         "override_data": "msg",
>         "x": 830,
>         "y": 340,
>         "wires": [
>             [
>                 "168d5252b24528fa"
>             ],
>             []
>         ]
>     },
>     {
>         "id": "d6464ebf67ce3b53",
>         "type": "api-current-state",
>         "z": "8050f576488419f2",
>         "name": "Brightness +",
>         "server": "f6a3415e.59246",
>         "version": 3,
>         "outputs": 2,
>         "halt_if": "on",
>         "halt_if_type": "str",
>         "halt_if_compare": "is",
>         "entity_id": "light.lamp_filament_test",
>         "state_type": "str",
>         "blockInputOverrides": false,
>         "outputProperties": [
>             {
>                 "property": "payload",
>                 "propertyType": "msg",
>                 "value": "",
>                 "valueType": "entityState"
>             },
>             {
>                 "property": "data",
>                 "propertyType": "msg",
>                 "value": "",
>                 "valueType": "entity"
>             },
>             {
>                 "property": "brightness",
>                 "propertyType": "msg",
>                 "value": "$entity().attributes.brightness+20",
>                 "valueType": "jsonata"
>             }
>         ],
>         "for": "0",
>         "forType": "num",
>         "forUnits": "minutes",
>         "override_topic": false,
>         "state_location": "payload",
>         "override_payload": "msg",
>         "entity_location": "data",
>         "override_data": "msg",
>         "x": 830,
>         "y": 280,
>         "wires": [
>             [
>                 "168d5252b24528fa"
>             ],
>             []
>         ]
>     },
>     {
>         "id": "f6a3415e.59246",
>         "type": "server",
>         "name": "Home Assistant",
>         "addon": true
>     }
> ]

Thank you for this example
But i get an error
Custom Ouput Error (msg:brightness ): Error: Invalid property expression: unexpected ’ ’ at position 10

JSONataError: Expected “:”, got “}”

Can you help me?

my guess is that you have an incorrect json format, can you paste what you have under the brightness?

I found the error.

Can you show the screenshot of the Button? triggerId is not an option for msg.topic in my NodeRed I can see he detects the button, but the switch has no input afterwards according to debug

Hello mkoch66, this is the pull down value from the button state node

1 Like

Thanks, this helped.

For anyone else here: Here’s the node red Json code with all the “>” removed so you don’t need to spend 5 minutes deleting them all manually like I just had to to get it to import.

[
     {
         "id": "8050f576488419f2",
         "type": "tab",
         "label": "test flows",
         "disabled": false,
         "info": "",
         "env": []
     },
     {
         "id": "4118cf0cb723bd54",
         "type": "server-state-changed",
         "z": "8050f576488419f2",
         "name": "Hue Button",
         "server": "f6a3415e.59246",
         "version": 5,
         "outputs": 1,
         "exposeAsEntityConfig": "",
         "entityId": "sensor.philips_dimmer_action",
         "entityIdType": "exact",
         "outputInitially": false,
         "stateType": "str",
         "ifState": "",
         "ifStateType": "str",
         "ifStateOperator": "is",
         "outputOnlyOnStateChange": true,
         "for": "0",
         "forType": "num",
         "forUnits": "minutes",
         "ignorePrevStateNull": false,
         "ignorePrevStateUnknown": false,
         "ignorePrevStateUnavailable": false,
         "ignoreCurrentStateUnknown": false,
         "ignoreCurrentStateUnavailable": false,
         "outputProperties": [
             {
                 "property": "payload",
                 "propertyType": "msg",
                 "value": "",
                 "valueType": "entityState"
             },
             {
                 "property": "data",
                 "propertyType": "msg",
                 "value": "",
                 "valueType": "eventData"
             },
             {
                 "property": "topic",
                 "propertyType": "msg",
                 "value": "",
                 "valueType": "triggerId"
             }
         ],
         "x": 320,
         "y": 320,
         "wires": [
             [
                 "4d199824eca8c556"
             ]
         ]
     },
     {
         "id": "4d199824eca8c556",
         "type": "switch",
         "z": "8050f576488419f2",
         "name": "",
         "property": "payload",
         "propertyType": "msg",
         "rules": [
             {
                 "t": "eq",
                 "v": "on_press",
                 "vt": "str"
             },
             {
                 "t": "eq",
                 "v": "up_press",
                 "vt": "str"
             },
             {
                 "t": "eq",
                 "v": "down_press",
                 "vt": "str"
             },
             {
                 "t": "eq",
                 "v": "off_press",
                 "vt": "str"
             }
         ],
         "checkall": "true",
         "repair": false,
         "outputs": 4,
         "x": 490,
         "y": 320,
         "wires": [
             [
                 "d463aa089066fd28"
             ],
             [
                 "d6464ebf67ce3b53"
             ],
             [
                 "be094d66f2abae65"
             ],
             [
                 "c819a54ce4380ea7"
             ]
         ]
     },
     {
         "id": "d463aa089066fd28",
         "type": "api-call-service",
         "z": "8050f576488419f2",
         "name": "",
         "server": "f6a3415e.59246",
         "version": 5,
         "debugenabled": false,
         "domain": "light",
         "service": "turn_on",
         "areaId": [],
         "deviceId": [
             "4a5d33b455c5d987e1e127740efe4dfe"
         ],
         "entityId": [],
         "data": "",
         "dataType": "jsonata",
         "mergeContext": "",
         "mustacheAltTags": false,
         "outputProperties": [],
         "queue": "none",
         "x": 830,
         "y": 220,
         "wires": [
             []
         ]
     },
     {
         "id": "c819a54ce4380ea7",
         "type": "api-call-service",
         "z": "8050f576488419f2",
         "name": "",
         "server": "f6a3415e.59246",
         "version": 5,
         "debugenabled": false,
         "domain": "light",
         "service": "turn_off",
         "areaId": [],
         "deviceId": [
             "4a5d33b455c5d987e1e127740efe4dfe"
         ],
         "entityId": [],
         "data": "",
         "dataType": "jsonata",
         "mergeContext": "",
         "mustacheAltTags": false,
         "outputProperties": [],
         "queue": "none",
         "x": 830,
         "y": 400,
         "wires": [
             []
         ]
     },
     {
         "id": "168d5252b24528fa",
         "type": "api-call-service",
         "z": "8050f576488419f2",
         "name": "Turn brightness up/down",
         "server": "f6a3415e.59246",
         "version": 5,
         "debugenabled": true,
         "domain": "light",
         "service": "turn_on",
         "areaId": [],
         "deviceId": [
             "4a5d33b455c5d987e1e127740efe4dfe"
         ],
         "entityId": [],
         "data": "{\"brightness\":\"{{brightness}}\"} ",
         "dataType": "json",
         "mergeContext": "",
         "mustacheAltTags": false,
         "outputProperties": [],
         "queue": "none",
         "x": 1070,
         "y": 300,
         "wires": [
             []
         ]
     },
     {
         "id": "be094d66f2abae65",
         "type": "api-current-state",
         "z": "8050f576488419f2",
         "name": "Brightness -",
         "server": "f6a3415e.59246",
         "version": 3,
         "outputs": 2,
         "halt_if": "on",
         "halt_if_type": "str",
         "halt_if_compare": "is",
         "entity_id": "light.lamp_filament_test",
         "state_type": "str",
         "blockInputOverrides": false,
         "outputProperties": [
             {
                 "property": "payload",
                 "propertyType": "msg",
                 "value": "",
                 "valueType": "entityState"
             },
             {
                 "property": "data",
                 "propertyType": "msg",
                 "value": "",
                 "valueType": "entity"
             },
             {
                 "property": "brightness",
                 "propertyType": "msg",
                 "value": "$entity().attributes.brightness-20",
                 "valueType": "jsonata"
             }
         ],
         "for": "0",
         "forType": "num",
         "forUnits": "minutes",
         "override_topic": false,
         "state_location": "payload",
         "override_payload": "msg",
         "entity_location": "data",
         "override_data": "msg",
         "x": 830,
         "y": 340,
         "wires": [
             [
                 "168d5252b24528fa"
             ],
             []
         ]
     },
     {
         "id": "d6464ebf67ce3b53",
         "type": "api-current-state",
         "z": "8050f576488419f2",
         "name": "Brightness +",
         "server": "f6a3415e.59246",
         "version": 3,
         "outputs": 2,
         "halt_if": "on",
         "halt_if_type": "str",
         "halt_if_compare": "is",
         "entity_id": "light.lamp_filament_test",
         "state_type": "str",
         "blockInputOverrides": false,
         "outputProperties": [
             {
                 "property": "payload",
                 "propertyType": "msg",
                 "value": "",
                 "valueType": "entityState"
             },
             {
                 "property": "data",
                 "propertyType": "msg",
                 "value": "",
                 "valueType": "entity"
             },
             {
                 "property": "brightness",
                 "propertyType": "msg",
                 "value": "$entity().attributes.brightness+20",
                 "valueType": "jsonata"
             }
         ],
         "for": "0",
         "forType": "num",
         "forUnits": "minutes",
         "override_topic": false,
         "state_location": "payload",
         "override_payload": "msg",
         "entity_location": "data",
         "override_data": "msg",
         "x": 830,
         "y": 280,
         "wires": [
             [
                 "168d5252b24528fa"
             ],
             []
         ]
     },
     {
         "id": "f6a3415e.59246",
         "type": "server",
         "name": "Home Assistant",
         "addon": true
     }
 ]

Thanks so much for this. It will make my life much easier.

Have you found a way to use the double press and Long press functionality of these switches?

it should be quite easy, put a debug node in node red behind the node to read the long or double press, and you can see what it returns, then you can make it actionable.

1 Like