Multiple state toggle with Node-Red
This is one of examples where easy WYSIWTF editor is simply not enough.
What this flow is doing:
- Trigger on action from button
- Check what type of actions is used (single, double or long)
- Get state of light entity
- Check if light is on
- If yes set brightness accordingly, if no simply turn it on
Do you see API Error. I haven't found that writing my last post. Solution was simple - no brightness level were provided.
Another neat feature: light entity is defined only in current_state node. After that I can reuse this referring to future entities as {{data.entity_id}}
as payload from currrent_state contains this exact info
And another one. Why I chose to dim lights before turn it off? This was because of calling light.turn_on
service directly on HA simply omit brightness parameter if previous state was off. And as I checked it right now I don't need to as turn on with brightness works as it should. Sweet.
Unfortunately my Tuya lights still ignores transition values.
Code
[
{
"id": "097d75dc7195cb92",
"type": "tab",
"label": "Przycisk",
"disabled": false,
"info": "",
"env": []
},
{
"id": "3d9aec0b01c255ad",
"type": "server-state-changed",
"z": "097d75dc7195cb92",
"name": "button_action",
"server": "1b427ab1.8760e5",
"version": 4,
"exposeToHomeAssistant": false,
"haConfig": [
{
"property": "name",
"value": ""
},
{
"property": "icon",
"value": ""
}
],
"entityidfilter": "sensor.przycisk_action",
"entityidfiltertype": "exact",
"outputinitially": false,
"state_type": "str",
"haltifstate": "single, double, long",
"halt_if_type": "str",
"halt_if_compare": "includes",
"outputs": 2,
"output_only_on_state_change": true,
"for": "0",
"forType": "num",
"forUnits": "minutes",
"ignorePrevStateNull": false,
"ignorePrevStateUnknown": false,
"ignorePrevStateUnavailable": false,
"ignoreCurrentStateUnknown": true,
"ignoreCurrentStateUnavailable": true,
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "entityState"
},
{
"property": "data",
"propertyType": "msg",
"value": "",
"valueType": "eventData"
},
{
"property": "topic",
"propertyType": "msg",
"value": "",
"valueType": "triggerId"
}
],
"x": 110,
"y": 360,
"wires": [
[
"caebef3b8c7e6c0e"
],
[]
]
},
{
"id": "caebef3b8c7e6c0e",
"type": "switch",
"z": "097d75dc7195cb92",
"name": "button_event_type",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "single",
"vt": "str"
},
{
"t": "eq",
"v": "double",
"vt": "str"
},
{
"t": "eq",
"v": "long",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 310,
"y": 360,
"wires": [
[
"2bcf4d6be6798032"
],
[
"992deb5019f833f8"
],
[
"ee2d3575b4e16f5e"
]
]
},
{
"id": "2bcf4d6be6798032",
"type": "api-current-state",
"z": "097d75dc7195cb92",
"name": "",
"server": "1b427ab1.8760e5",
"version": 3,
"outputs": 1,
"halt_if": "",
"halt_if_type": "str",
"halt_if_compare": "is",
"entity_id": "light.poddasze_sufit",
"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": 620,
"y": 160,
"wires": [
[
"fa529ed62017645e"
]
]
},
{
"id": "fa529ed62017645e",
"type": "switch",
"z": "097d75dc7195cb92",
"name": "light_on",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "on",
"vt": "str"
},
{
"t": "eq",
"v": "off",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 900,
"y": 160,
"wires": [
[
"3f97d15848b5bd41"
],
[
"6c51294eb6c18b19"
]
]
},
{
"id": "3f97d15848b5bd41",
"type": "switch",
"z": "097d75dc7195cb92",
"name": "brightness_level",
"property": "data.attributes.brightness",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "50",
"vt": "str"
},
{
"t": "btwn",
"v": "50",
"vt": "num",
"v2": "130",
"v2t": "num"
},
{
"t": "btwn",
"v": "130",
"vt": "num",
"v2": "256",
"v2t": "num"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 1100,
"y": 120,
"wires": [
[
"f351a2d67e0ea568"
],
[
"901b64bd1a4ccf10"
],
[
"edb3097c20c2502f"
]
]
},
{
"id": "6c51294eb6c18b19",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@1",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"1\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1090,
"y": 180,
"wires": [
[]
]
},
{
"id": "edb3097c20c2502f",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@off",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_off",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1310,
"y": 180,
"wires": [
[]
]
},
{
"id": "901b64bd1a4ccf10",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@255",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"255\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1320,
"y": 120,
"wires": [
[]
]
},
{
"id": "f351a2d67e0ea568",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@120",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"120\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1320,
"y": 60,
"wires": [
[]
]
},
{
"id": "992deb5019f833f8",
"type": "api-current-state",
"z": "097d75dc7195cb92",
"name": "",
"server": "1b427ab1.8760e5",
"version": 3,
"outputs": 1,
"halt_if": "",
"halt_if_type": "str",
"halt_if_compare": "is",
"entity_id": "light.poddasze_biurko",
"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": 630,
"y": 360,
"wires": [
[
"c5df1eb11fe3b2de"
]
]
},
{
"id": "c5df1eb11fe3b2de",
"type": "switch",
"z": "097d75dc7195cb92",
"name": "light_on",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "on",
"vt": "str"
},
{
"t": "eq",
"v": "off",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 900,
"y": 360,
"wires": [
[
"716c61d885d4e867"
],
[
"7770eb1bc8a2fe7f"
]
]
},
{
"id": "716c61d885d4e867",
"type": "switch",
"z": "097d75dc7195cb92",
"name": "brightness_level",
"property": "data.attributes.brightness",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "50",
"vt": "str"
},
{
"t": "btwn",
"v": "50",
"vt": "num",
"v2": "130",
"v2t": "num"
},
{
"t": "btwn",
"v": "130",
"vt": "num",
"v2": "256",
"v2t": "num"
}
],
"checkall": "false",
"repair": false,
"outputs": 3,
"x": 1100,
"y": 320,
"wires": [
[
"42f2fa3a880f8d60"
],
[
"40c438564b9eadab"
],
[
"740fd193a7191d63"
]
]
},
{
"id": "740fd193a7191d63",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@off",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_off",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1310,
"y": 380,
"wires": [
[
"36500fabb40cbc08"
]
]
},
{
"id": "40c438564b9eadab",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@255",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"255\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1320,
"y": 320,
"wires": [
[]
]
},
{
"id": "42f2fa3a880f8d60",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@120",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"120\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1320,
"y": 260,
"wires": [
[]
]
},
{
"id": "ee2d3575b4e16f5e",
"type": "api-current-state",
"z": "097d75dc7195cb92",
"name": "",
"server": "1b427ab1.8760e5",
"version": 3,
"outputs": 1,
"halt_if": "",
"halt_if_type": "str",
"halt_if_compare": "is",
"entity_id": "light.poddasze_lozko",
"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": 620,
"y": 680,
"wires": [
[
"bb4f0f47aeb1535d"
]
]
},
{
"id": "bb4f0f47aeb1535d",
"type": "switch",
"z": "097d75dc7195cb92",
"name": "light_on",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "on",
"vt": "str"
},
{
"t": "eq",
"v": "off",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 900,
"y": 680,
"wires": [
[
"93569611e5f3b60e"
],
[
"53155f3171a671e7"
]
]
},
{
"id": "93569611e5f3b60e",
"type": "switch",
"z": "097d75dc7195cb92",
"name": "brightness_level",
"property": "data.attributes.brightness",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "50",
"vt": "str"
},
{
"t": "gte",
"v": "50",
"vt": "str"
}
],
"checkall": "false",
"repair": false,
"outputs": 2,
"x": 1100,
"y": 640,
"wires": [
[
"ba4182e992f39a80"
],
[
"c9e0d9e6404d7acd"
]
]
},
{
"id": "c9e0d9e6404d7acd",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@off",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_off",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1310,
"y": 660,
"wires": [
[]
]
},
{
"id": "ba4182e992f39a80",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@120",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"120\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1320,
"y": 600,
"wires": [
[]
]
},
{
"id": "b07d286bbed59e97",
"type": "comment",
"z": "097d75dc7195cb92",
"name": "Also light entity definiton",
"info": "",
"x": 630,
"y": 100,
"wires": []
},
{
"id": "179a5e966d2a42ba",
"type": "comment",
"z": "097d75dc7195cb92",
"name": "Change according to integration",
"info": "",
"x": 310,
"y": 280,
"wires": []
},
{
"id": "c610086172fae911",
"type": "comment",
"z": "097d75dc7195cb92",
"name": "Button entity setup",
"info": "",
"x": 110,
"y": 440,
"wires": []
},
{
"id": "7770eb1bc8a2fe7f",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@1",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"1\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1090,
"y": 440,
"wires": [
[
"b574bc6378ebb763"
]
]
},
{
"id": "53155f3171a671e7",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_light@1",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "light",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"{{data.entity_id}}"
],
"data": "{\"brightness\":\"1\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1090,
"y": 700,
"wires": [
[]
]
},
{
"id": "b574bc6378ebb763",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_monitory@on",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"switch.poddasze_monitory"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1330,
"y": 440,
"wires": [
[]
]
},
{
"id": "36500fabb40cbc08",
"type": "api-call-service",
"z": "097d75dc7195cb92",
"name": "set_monitory@on",
"server": "1b427ab1.8760e5",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "turn_off",
"areaId": [],
"deviceId": [],
"entityId": [
"switch.poddasze_monitory"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1550,
"y": 380,
"wires": [
[]
]
},
{
"id": "1b427ab1.8760e5",
"type": "server",
"name": "Home Assistant",
"addon": true
}
]
Thats all for now.