myStrom Switch Control and State Update

Hi,

I am a beginner and am trying to integrate the myStrom Switch into my openhab environment and since I dont want to use a mystrom account I dont use the existing myStrom binding. I want to control the switch “offline” via openhab and additionally want to detect if the user changed the state by manual pressing the button on the device:

To Control the switch there are two easy http-get commands like http://IP/relay?state=1 (or 0). To get the state, the device creates a json file at http://IP/report like:
{
“power”: 28.355360,
“relay”: true
}

Switch control is working:

Switch myStrom "myStrom Steckdose" <poweron> (Switches) { http=">[ON:GET:http://192.168.168.20/relay?state=1] >[OFF:GET:http://192.168.168.20/relay?state=0]" }

Switch state is displayed in a separate “String” item on openhab (which is updated every second)

String myStrom_State "State:[%s]" (Switches) { http="<[http://192.168.168.20/report:1000:JSONPATH($.relay)]" }

But how do I integrate the state into my openhab-switch item? I tried adding

<[http://192.168.168.20/report:1000:JSONPATH($.relay)]

to my switch item but it does not seem to work. I guess I somehow have to transform “true” to ON and “false” to OFF.

I used mqtt bindings before where you can tranform the content of the mqtt package by adding ON:1 or OFF:1 at the end. But this doesnt seem to work either.

<[http://192.168.168.20/report:1000:JSONPATH($.relay):OFF:false]

Do you have any idea what I have to change to get it working?

thx in advance

You are already using a transformation with JSONPATH, I am afraid but you can’t nest them.
So you have two options

  • Use a String Item and a rule to “transform” the string to your switch Item
  • Use a JS transform

Option1

Items:

String myStromString { http="<[http://192.168.168.20/report:1000:JSONPATH($.relay)]"  }
Switch myStrom

Rules:

rule "String to switch"
when
    Item myStromString changed
then
    if (myStromString.state.toString == "true") {
        myStrom.sendCommand(ON)
    else { // assuming that there are only two states true/false
        myStrom.semdCommand(OFF)
    }
end

Option 2 Javascript transform

Create a file called mystrom.js in your transform folder
With the following content:

(function(i) {
    var data = JSON.parse(i);
    var relaystate = data.relay;
    if (relaystate == 'true') {
        result = 'ON'
    } else {
        result = 'OFF'
    }
    return result;
})(input)

Your item:

Swith myStrom { http="<[http://192.168.168.20/report:1000:JS(mystrom.js)]"  }

Good luck

Hi,

thank you very much. I really could have come up with the rule-based solution by myself, but I was so focused on bindings that I forgot about the rules. :man_facepalming:

So I implemented your rule and it works perfectly. :slight_smile:

thx again.