# Node-Red / Xiaomi

**URL:** <https://community.gladysassistant.com/t/node-red-xiaomi/6644>\
**Category:** Configuration\
**Created:** [October 30, 2021, 4:03pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644 "2021-10-30T16:03:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [October 30, 2021, 4:03pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/1 "2021-10-30T16:03:45Z")

</div>

Hello everyone,

With the arrival of Node-Red on Gladys, I was wondering if someone could help me configure my 2 double switches and 1 single switch to manage them via Gladys.

I followed Pierre Gilles’ video to the letter and so far, no issues.

I installed node-red-contrib-xiaomi-smart-devices to connect my Xiaomi devices and I can detect my switches and sensors.

However, I’m stuck on managing my single switch (to start before trying the doubles) to connect it to Gladys.

On the Gladys side, I created my MQTT topic and added my switch to the dashboard.

On the Node-red side, MQTT node created and connected, the switch node with the debug on/off also works. My Xiaomi device is also recognized and connected.

 ![Screenshot 2021-10-30 at 18.01.16](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/9/9fd94cf60e2acdc9fb4d6421e4457f2c227203ae.png)

I’m missing the link between the two, with the right function between the switch and the device I think.

A little help 😢 please. I specify that I am far from being an expert and even less Node-red, I am more of a DIYer.

Tell me the screenshots you will need.

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [November 1, 2021, 2:37am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/2 "2021-11-01T02:37:03Z")

</div>

Hello @jerome, let’s look at this together! 🙂

> [@jerome](#):
>
> I’m missing the link between the two, I think with the right function between the switch and the device.

Doesn’t it work just to connect the switch with your «&nbsp;bedroom switch&nbsp;» like I do in the video?

Can you turn your switch on/off from Node-RED?

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [November 1, 2021, 7:06am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/3 "2021-11-01T07:06:41Z")

</div>

Hello @pierre-gilles

No, I can’t turn the switch on/off either. It’s connected, but when I connect a switch or an injector, there’s no result. Here’s what is specified in Node-RED:

### [Inputs](http://192.168.0.8:1880/#)

payload.channel\_0. _string_

`on` - if you want to turn on  
`off` - turn off  
`switch` - change state on → off, off → on

payload.channel\_1 _string_

`on` - if you want to turn on  
`off` - turn off  
`switch` - change state on → off, off → on

### [Outputs](http://192.168.0.8:1880/#)

Passes the complete json object received from the node to the output

Sample message of Wired Single-Button:

{ «&nbsp;fromip&nbsp;»:«&nbsp;192.168.0.110:4321&nbsp;», «&nbsp;ip&nbsp;»:«&nbsp;192.168.0.110&nbsp;», «&nbsp;port&nbsp;»:4321, «&nbsp;payload&nbsp;»:{ «&nbsp;channel\_0&nbsp;»:«&nbsp;on&nbsp;», «&nbsp;time&nbsp;»:1556987229523, «&nbsp;sid&nbsp;»:«&nbsp;158d000128f9dd&nbsp;», «&nbsp;model&nbsp;»:«&nbsp;ctrl\_ln1&nbsp;» } }

Sample message of Wireless Double-Buttons:

{ «&nbsp;fromip&nbsp;»:«&nbsp;192.168.0.110:4321&nbsp;», «&nbsp;ip&nbsp;»:«&nbsp;192.168.0.110&nbsp;», «&nbsp;port&nbsp;»:4321, «&nbsp;payload&nbsp;»:{ «&nbsp;channel\_1&nbsp;»:«&nbsp;click&nbsp;», «&nbsp;time&nbsp;»:1556987769175, «&nbsp;sid&nbsp;»:«&nbsp;158d000242275f&nbsp;», «&nbsp;model&nbsp;»:«&nbsp;86sw2&nbsp;» } }

Do I need to add a function in between? I probably need to specify channel\_0 somewhere?

Thanks for your help because I’ve been looking for tutorials or information on the internet and I haven’t found much about node-red-contrib-xiaomi-smart-devices.

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [November 1, 2021, 9:12am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/4 "2021-11-01T09:12:56Z")

</div>

You need to use an «&nbsp;Inject&nbsp;» node

 ![Screenshot 2021-11-01 at 17.11.44](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/9/9bdbd9570f85449fa421bf91b1e63d5ae2bf041d.png)

You can inject data into the payload:

 ![Screenshot 2021-11-01 at 17.11.15](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/f/f14a8ab9d6ddcf22123aeeab62866e766fdb07e3.png)

Then you connect your «&nbsp;inject&nbsp;» node to your xiaomi node

You can test if it works by clicking the button on the «&nbsp;inject&nbsp;» node 🙂

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [November 1, 2021, 9:35am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/5 "2021-11-01T09:35:28Z")

</div>

I tried this operation but nothing happens. In debug mode, I get this message:

msg : string[62]  
«&nbsp;Error: Invalid property expression: unexpected at position 8&nbsp;»

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [November 1, 2021, 9:37am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/6 "2021-11-01T09:37:04Z")

</div>

Can you send me a screenshot of your inject node + a JSON export if possible?

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [November 1, 2021, 9:45am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/7 "2021-11-01T09:45:31Z")

</div>

The error I found is because I tested with the space after payload. channel\_0 like your screenshot seemed weird to me.

But I had tested without it before, same no debug return though

 ![Capture d’écran 2021-11-01 à 10.43.06](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/9/911986fd7597830d9427b1506540fd831549b682.png)

 ![Capture d’écran 2021-11-01 à 10.43.27](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/d/d7cfa070b297e2754ed30c7ab5dae1f4c8d5c7ec.png)

Export the json??? Is that what’s missing between the two?

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [November 2, 2021, 1:23am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/8 "2021-11-02T01:23:47Z")

</div>

> [@jerome](#):
>
> The error I found is because I tested with a space after payload. channel\_0 like your screenshot seemed weird to me.

Oh no, but there is no space after `payload.`, it’s my mistake, I just took a screenshot quickly ^^

You can put your debug right after your inject to see what your payload looks like?

> [@jerome](#):
>
> Export the json??? Is that what I’m missing between the two?

No, I was just asking for an export so I can see what you’re doing. It’s not a big deal, the screenshot is fine too 🙂

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [November 2, 2021, 10:46pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/9 "2021-11-02T22:46:34Z")

</div>

Hello @pierre-gilles

Thanks to debugging, I can finally turn the light on and off via my switch

Here is the result:

 ![Capture d’écran 2021-11-02 à 23.39.21](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/8/86f92896abe2932b726d51f316f08c3fb0eb37e4.png)

I will try to control it via Gladys now

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [November 3, 2021, 1:16am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/10 "2021-11-03T01:16:03Z")

</div>

Great! Keep us updated, and if you can, when you get there, make a small tutorial in the «&nbsp;Tutorials&nbsp;» category of the forum, that will help other people!

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [November 29, 2021, 9:21am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/11 "2021-11-29T09:21:33Z")

</div>

I’m a bit stuck here.

I can’t figure out how to connect MQTT to the actuator for my Xiaomi switch.

Phase 1: As in the video, OK in debug I receive the value 0 and 1  
Phase 2: With the input I can turn the light on or off

However, between the switch and the actuator, what node should I add? I thought about using a function, but since I’m not very familiar with coding, it’s not straightforward.

 ![Screenshot 2021-11-29 at 10.19.22](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/1/1b986fe9c74ddd7bd10138e476118c0221a134ea.png)

For me, it should be told that:  
msg.payload = msg.payload.channel\_0  
that 0 = Off and 1 = On  
and that the device is the SID: …

Am I on the wrong track or not?

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [November 29, 2021, 1:06pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/12 "2021-11-29T13:06:22Z")

</div>

> [@jerome](#):
>
> However, between the switch and the shareholder, which node should I add?

From the switch to the «&nbsp;Off&nbsp;» button, you just need to draw a line with your mouse, all the work is already done 😃 You’re really just 1 millimeter away from the result haha 🙂

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [November 29, 2021, 1:25pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/13 "2021-11-29T13:25:00Z")

</div>

I’ve already tried and it’s not possible with these two outputs. I don’t have an input on the inject.

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [November 29, 2021, 2:08pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/14 "2021-11-29T14:08:41Z")

</div>

Oh my bad! I know there’s a node that does the same as the inject but with an input

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [November 29, 2021, 3:09pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/15 "2021-11-29T15:09:54Z")

</div>

There is the switch or the button in the dashboard but I can’t set them up.

 ![Capture d’écran 2021-11-29 à 16.08.29](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/8/80c8fdbd398a63bb5fb579ca056a88f951ad2b6d.png)

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [November 29, 2021, 3:17pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/16 "2021-11-29T15:17:15Z")

</div>

For me, the switch is useless, you just need to format the payload if necessary for your actuator.

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [December 1, 2021, 10:36pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/17 "2021-12-01T22:36:53Z")

</div>

Format the payload? You lost me, sorry 😅

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [December 2, 2021, 6:27am UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/18 "2021-12-02T06:27:21Z")

</div>

If msg.payload = 1 then msg.payload.channel0 = on

What your actuator receives is the payload (which must be in the correct format)

You can perform this transformation with a function block

> **[Function - The Core Nodes : Node-RED](https://nodered.org/docs/user-guide/nodes#function)**
>
> The Node-RED palette includes a default set of nodes that are the basic building blocks for creating flows. This page highlights the core set you should know about.

```js
if (msg.payload === 0) {
msg.payload.channel0 = off
} else {
.....

```

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [December 2, 2021, 11:07pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/19 "2021-12-02T23:07:24Z")

</div>

Something like this?

> if (msg.payload === 0) {  
> var cmd = {  
> «&nbsp;sid&nbsp;» : «&nbsp;XXXXXXXXXXX&nbsp;»  
> }  
> msg.payload.channel0 = off  
> }  
> else { (msg.payload === 1)  
> msg.payload.channel0 = on  
> }  
> return msg;

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [December 6, 2021, 2:30pm UTC](https://community.gladysassistant.com/t/node-red-xiaomi/6644/20 "2021-12-06T14:30:13Z")

</div>

Sinon @jerome @VonOx, the «&nbsp;Change&nbsp;» function might be more suitable if you’re not comfortable with the code 🙂 [The Core Nodes : Node-RED](https://nodered.org/docs/user-guide/nodes#change)

 ![Screenshot 2021-12-06 at 15.28.12](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/1/19bd427a858dd534a0b2d2318724207f0e8a6859.jpeg)

In your case, you can place it after your switch, and do a «&nbsp;set&nbsp;» where you set the correct property, I don’t know if that’s clear for you @jerome …

[Next page](https://community.gladysassistant.com/t/node-red-xiaomi/6644.md?page=2)
