One Button toggle to turn light on and off

Home Assistant and Node Red novice

I have a Ikea Styrbar controller and Ikea light bulb that I have configured in mqtt and this is working great.

Using Events:Status and Service Node (Toggle) I can toggle the light on and off but I see people using the MQTT nodes and can also see a lot more control over devices. (

So - I have setup a MTQQ in Node Red -

MTQQ IN - Message - Function (msg.payload = {"state": "ON"}) - MTQQ OUT (using SET after the topic name) and this is working great.

What I would like to do is press the same button to now turn off the light and I just can't see it

Searching for this seams to bring up all manor of stuff that I can't quite work out yet.

Can anyone point me in a general direction please.

(On a different matter, coming from Smartthings, I can't get over how quick this is)

Thanks

You will need to store the previous state so that you can send the opposite state.

Use context for this.

e.g.

chrome_dR7B1kkz48

[{"id":"83746a428adcbc17","type":"inject","z":"7eecf5f1d763605a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":810,"y":432,"wires":[["056008d289d9ada1"]]},{"id":"056008d289d9ada1","type":"function","z":"7eecf5f1d763605a","name":"toggle on/off","func":"var state = context.get(\"state\") || false;\nstate = !state;\ncontext.set(\"state\", state);\nif (state) {\n    msg.payload = {payload: \"ON\"}\n} else {\n    msg.payload = { payload: \"OFF\" }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":432,"wires":[["52182669c4a415b7"]]},{"id":"52182669c4a415b7","type":"debug","z":"7eecf5f1d763605a","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1180,"y":432,"wires":[]}]
1 Like

Hello and welcome to the forum.

Here's a link to a worksheet I created for my IoT students. It shows how to create various buttons.

Having fun with buttons

3 Likes

Many thanks to both of you. I managed to work it out from the worksheet and its working.

I had to change the payload to payload.action and update the code as that what the Ikea button sends over.

Glad to hear you got it working so quickly.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.