Ankan
(Anders)
December 4, 2018, 11:05am
1
I want to create a mqtt outbound message with json content that my Domoticz server understand.
Domoticz need json content where it can get idx and new state of switch like this:
{"command": "switchlight", "idx": 2450, "switchcmd": "On" }
The idx is unique like name in items on openHAB. So I was thinking of including idx in name on openHAB.
On and Off is case sensitive so ON and OFF will not work.
I tried to create this message with javascript and use it in my items-file like this:
[myBroker:domoticz/in:state:*:JS(set_switch.js)]
The problem I have is to get the name of the switch in the javascript. I guess I can’t add more to the javascript function argument?
Is javascript the way to go or how do I solve it?
rlkoshak
(Rich Koshak)
December 4, 2018, 5:09pm
2
I think you might need to do this in a Rule and use Proxy Items.
Dim
(Angelos)
December 4, 2018, 5:13pm
3
Here is one example (there are many in the forum) of a rule sending a json payload to mqtt:
Easy:
rule "heater time adjust Mode 2"
when
Item progr received command
then
//I want to change the payload values like Arm 1 or 0, Mode 1 or 2 etc
//but how can i create/compose the json string to send it? because i can't send it with this form above like a simple string
var jsonString = "{\"Arm\":" + ArmItem.state.toString
jsonString = jsonString + ",\"Mode\":" + modeItem.state.toString
jsonString = jsonString + ",\"Time\":\"-2:23\",\"Window\":0,\"Days\":\"11TW11S\",\"Repeat\":1,\"Output\":…
State is usually used in an inbound configuration in OH2. If you want to send the json payload you will need an outbound config.
See more here: https://www.openhab.org/addons/bindings/mqtt1/#item-configuration-for-outbound-messages
Ankan
(Anders)
December 4, 2018, 6:44pm
4
Do I have to write one rule for every item? Is it possible to make it universal as I want to do same thing everytime on all switches.
A example would be great with mqtt outbound message as I have hard to find something simular and have never used rules.
How do I get item name, state and so on in the rule?
And what do you meen with Proxy item?
rlkoshak
(Rich Koshak)
December 4, 2018, 7:36pm
5
Yes, this is possible and encouraged. I recommend looking at Design Pattern: Human Readable Names in Messages and Design Pattern: Associated Items for ways to achieve this.
Ankan
(Anders)
December 6, 2018, 9:31pm
6
Okey, This is how I solved it. Maybe it’s possible to do it even better.
rule "Send MQTT message on Light command"
when
Member of gLight received command
then
var idxStartPos = triggeringItem.name.indexOf("idx") + 3
var idxStopPos = triggeringItem.name.length()
var myString = ""
if (triggeringItem.state.toString() == "ON" || triggeringItem.state.toString() == "OFF")
{
var cmd = triggeringItem.state.toString().toLowerCase.toFirstUpper()
// {"command": "switchlight", "idx": 2450, "switchcmd": "On" }
myString = '{\"command\": \"switchlight\", \"idx\": ' + triggeringItem.name.substring(idxStartPos,idxStopPos) + ', \"switchcmd\": \"' + cmd + '\"}'
}
else
{
// {"command": "switchlight", "idx": 2450, "switchcmd": "Set Level", "level": 100 }
myString = '{\"command\": \"switchlight\", \"idx\": ' + triggeringItem.name.substring(idxStartPos,idxStopPos) + ', \"switchcmd\": \"Set Level\", \"level\": ' + triggeringItem.state.toString() + '}'
}
publish("mqttBroker", "domoticz/in", myString)
end
Switch idx123 "Outdoorlamp" (gLight) [ "Lighting" ] {mqtt="<[mqttBroker:domoticz/out:state:JS(get_state.js):(?s).*idx\".?.?.?123,.*]"}
Number Outdoor_Temperature "Temperature [%.1f]" <temperature> ["CurrentTemperature"] {mqtt="<[mqttBroker:domoticz/out:state:JSONPATH($.svalue1):(?s).*idx\".?.?.?124,.*]"}
(function(i){
var parsed = JSON.parse(i);
if (parsed.nvalue == 0)
{
return "OFF"
}
else if (parsed.nvalue == 1)
{
return "ON"
}
else if (parsed.nvalue == 2)
{
return parsed.svalue1
}
})(input);