Page 1 of 1
Problem with translation MQTT transmision by Node-red
Posted: Sunday 23 February 2020 20:56
by rkarolek
hello
i have problem with transalate my MQTT device's to domoticz by node red.
my device publish - "dom/salon/gniazdko/temperature 33.13"
i translate it by node red to - "domoticz/in {"command":"udevice","idx":252,"svalue":33.13}"
but its not works at all

in domoticz log i can see
"2020-02-23 20:55:17.625 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":252,"svalue":33.13}
2020-02-23 20:55:17.625 Error: MQTT: Invalid data received!"
what im doing wrong??
Please help
Re: Problem with translation MQTT transmision by Node-red
Posted: Sunday 23 February 2020 21:36
by McMelloW
Re: Problem with translation MQTT transmision by Node-red
Posted: Sunday 23 February 2020 21:47
by rkarolek
already read this topics, i followed step by step and as i said i translate my device to domoticz format, but.... its not works
so i think i do any stupid mistake and why i cant solve it

Re: Problem with translation MQTT transmision by Node-red
Posted: Sunday 23 February 2020 22:10
by FireWizard
Hi,
Change the command to: {"command":"udevice","idx":252,"svalue":"33.13"}
Mind the position of " ". The value, you want to send to Domoticz (svalue) has to be a string.
Regards
Re: Problem with translation MQTT transmision by Node-red
Posted: Monday 24 February 2020 17:18
by rkarolek
works!
tyvm!
Re: Problem with translation MQTT transmision by Node-red
Posted: Saturday 07 March 2020 14:04
by rkarolek
new problem

translate temperature works well, so i try add switch controll too
now my transalte function looks like:
Code: Select all
switch (msg.topic) {
case "dom/salon/gniazdko/temperature":
msg.payload = {"command":"udevice","idx":252,"nvalue":0,"svalue":msg.payload};
break;
case "dom/salon/gniazdko/gniazdko/state":
msg.payload = {"command":"switchlight","idx":249,"switchcmd":msg.payload};
break;
}
return msg;
switching command are translate to:
domoticz/in {"command":"switchlight","idx":249,"switchcmd":"on"}
but my switch in domoticz dont works

in domoticz log i can find such note:
2020-03-07 14:02:22.178 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":249,"switchcmd":"on"}
2020-03-07 14:02:22.179 Error: MQTT: Error sending switch command!
i cant understand why switch not works, where is mistake??
maybe problemis that in translate command are "on"/"off", but domoticz need "On"/"Off"??? if yes how i should change my function?
ty in advance for any hints
Re: Problem with translation MQTT transmision by Node-red
Posted: Saturday 07 March 2020 14:38
by FireWizard
Hi,
Please Note:
From the wiki:
https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s
Turn a light/switch off
/json.htm?type=command¶m=switchlight&idx=99&switchcmd=Off
idx = id of your device (in this example 99).
switchcmd = "On" or "Off" (case sensitive!)
So change: domoticz/in {"command":"switchlight","idx":249,"switchcmd":"on"}
to: domoticz/in {"command":"switchlight","idx":249,"switchcmd":"On"}
Regards
Re: Problem with translation MQTT transmision by Node-red
Posted: Saturday 07 March 2020 14:41
by rkarolek
how i can change "on" to "On"??
sry if it stupid question, but im really new in this
EDIT
i change my function
Code: Select all
case "dom/salon/gniazdko/gniazdko/state":
if (msg.payload == "on")
{
msg.payload = {"command":"switchlight","idx":251,"switchcmd":"On"};
}
else
{
msg.payload = {"command":"switchlight","idx":251,"switchcmd":"Off"};
}
break;
it works, but maybe it should be done in better way?
Re: Problem with translation MQTT transmision by Node-red
Posted: Saturday 07 March 2020 18:37
by FireWizard
Hi,
The way you do it, is one possibility.
I assume that your payload is either "on" or "off" and you have to change that to "On" or "Off".
You can follow your first approach:
Code: Select all
switch (msg.topic) {
case "dom/salon/gniazdko/temperature":
msg.payload = {"command":"udevice","idx":252,"nvalue":0,"svalue":msg.payload};
break;
case "dom/salon/gniazdko/gniazdko/state":
msg.payload = {"command":"switchlight","idx":249,"switchcmd":msg.payload};
break;
}
return msg;
and change that to:
Code: Select all
switch (msg.topic) {
case "dom/salon/gniazdko/temperature":
msg.payload = {"command":"udevice","idx":252,"nvalue":0,"svalue":msg.payload};
break;
case "dom/salon/gniazdko/gniazdko/state":
msg.payload = {"command":"switchlight","idx":249,"switchcmd":(msg.payload.charAt(0).toUpperCase() + msg.payload.slice(1))};
break;
}
return msg;
Some explanation:
I took the first character of the payload (either "on" or "off"). The first character starts at 0 and therefore charAt(0) and only this character is converted to Upper case. And then I added, with the + sign, the remaining of the payload, starting from the second character (1)
So this function converts the first character of every string starting with a lowercase alphabet character to an uppercase character.
Try it,
Regards
Re: Problem with translation MQTT transmision by Node-red
Posted: Saturday 07 March 2020 19:04
by rkarolek
my version is simply,
your solution loks much much smart, and elegant, and propably idiot resistant
tyvm for help