Simple On/Off Switch with Exec-Command

sure - I will test it again the next days and keep you updated.
edit: I’m lacking the 330ohm-resistor. I’ll try next week.

I’m having the exact same issue, finding it difficult to migrate from OH1 to OH2 and get my 433mhz switches working with the exec binding.

@SnitzelWeck92 could you detail your exact setup / fix? I’m finding it difficult pinpointing the exact solution in the posts you’ve referenced.

hello Albert,
i will post my setting:

.things

//SteckdoseG1
Thing exec:command:steckdoseG1-control [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose1.sh %2$s", interval=0, autorun=true ]
Thing exec:command:steckdoseG1-status [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose1.sh status", interval=0, timeout=5 ]

//SteckdoseG2
Thing exec:command:steckdoseG2-control [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose2.sh %2$s", interval=0, autorun=true ]
Thing exec:command:steckdoseG2-status [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose2.sh status", interval=0, timeout=5 ]

//SteckdoseG3
Thing exec:command:steckdoseG3-control [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose3.sh %2$s", interval=0, autorun=true ]
Thing exec:command:steckdoseG3-status [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose3.sh status", interval=0, timeout=5 ]

//SteckdoseG4
Thing exec:command:steckdoseG4-control [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose4.sh %2$s", interval=0, autorun=true ]
Thing exec:command:steckdoseG4-status [ command="/opt/rc-switch/433Utils/RPi_utils/steckdose4.sh status", interval=0, timeout=5 ]

.items

Group grp_steckdosen "Steckdosen" <poweroutlet>

String steckdoseG1Switch "Steckdose G1" <poweroutlet> (grp_steckdosen) [ "Switchable" ] { channel="exec:command:steckdoseG1-control:input", channel="exec:command:steckdoseG1-status:output", autoupdate="true" }
String steckdoseG2Switch "Steckdose G2" <poweroutlet> (grp_steckdosen) [ "Switchable" ] { channel="exec:command:steckdoseG2-control:input", channel="exec:command:steckdoseG2-status:output", autoupdate="true" }
String steckdoseG3Switch "Steckdose G3" <poweroutlet> (grp_steckdosen) [ "Switchable" ] { channel="exec:command:steckdoseG3-control:input", channel="exec:command:steckdoseG3-status:output", autoupdate="true" }
String steckdoseG4Switch "Steckdose G4" <poweroutlet> (grp_steckdosen) [ "Switchable" ] { channel="exec:command:steckdoseG4-control:input", channel="exec:command:steckdoseG4-status:output", autoupdate="true" }

.sitemap

sitemap steckdosen label="Steckdosen" {
    Frame label="Funksteckdosen Gruppe G" {
        Switch item=steckdoseG1Switch mappings=[ "ON"="ON", "OFF"="OFF" ]
        Switch item=steckdoseG2Switch mappings=[ "ON"="ON", "OFF"="OFF" ]
        Switch item=steckdoseG3Switch mappings=[ "ON"="ON", "OFF"="OFF" ]
        Switch item=steckdoseG4Switch mappings=[ "ON"="ON", "OFF"="OFF" ]
        
        Switch item=grp_steckdosen mappings=[ "ON"="ON", "OFF"="OFF" ]
        
        Switch item=Dash_Switch
        }
}

.sh

#!/bin/sh


if [ "$1" = "off" ] || [ "$1" = "0" ] || [ "$1" = "OFF"  ]; then
        /opt/rc-switch/433Utils/RPi_utils/SteckdoseG1 15 0
else
        /opt/rc-switch/433Utils/RPi_utils/SteckdoseG1 15 1
fi

The Installation of rc-switch an wiring-pi is like here:
https://tutorials-raspberrypi.de/raspberry-pi-funksteckdosen-433-mhz-steuern/4

I hope i can help you with my summary…

2 Likes

Ok, so now I tested different approaches, so basically I got through this one again - but without the transistors:

Whats was the key, I don’t know, but basically what I did was:

  1. putting in a Raspbian SDCard (could also be a openHABian, I guess - but not tested with that one as this PI isn’t near the 433 devices)
  2. connecting the Sender and Receiver basically as shown in this pic but I connected the FS1000A (Sender) to the 3V PIN
  3. Installing an configuring the Software as described in Raspberry Pi - Pilight mit 433MHz Modulen zur Hausautomatisierung
  4. So, whenever I pushed a button at the remote, I got the payload via pilight-receive. and could use that in the config.json and with that I can control the outlets.

e.g. I got this sent after pressing the remote:

pi@raspberrypi3 ~ $ pilight-receive
        "message": {
                "id": "A2",
                "unit": 12,
                "state": "on"
        },
        "origin": "receiver",
        "protocol": "clarus_switch",
        "uuid": "0000-b1-23-eb-45678a",
        "repeats": 3

In that case, you can change the /etc/pilight/config.json:

{
        "devices": {
 
                "Switch1": {
                        "protocol": [ "clarus_switch" ], // 'protocol' of payload-message: be sure it's listed here: https://wiki.pilight.org/doku.php/protocols 
                        "id": [{
                                "id": "A2", // 'id' of payload-message
                                "unit": 12 // 'unit' of payload-message
                        }],
                        "state": "on"
                }

        },
        "rules": {},
        "gui": {
                "Switch1": {
                        "name": "Switch Nr 1",
                        "group": [ "MeineSwitches" ],
                        "media": [ "all" ]
                }
 
        },
        "settings": {
                "log-level": 4,
                "pid-file": "/var/run/pilight.pid",
                "log-file": "/var/log/pilight.log",
                "receive-repeats": 1,
                "webserver-enable": 1,
                "webserver-root": "/usr/local/share/pilight/",
                "webserver-port": 80,
                "webserver-cache": 1
        },
        "hardware": {
                "433gpio": {
                        "sender": 0,
                        "receiver": 1
                }
        },
        "registry": {}
}

so the Attributes you’re looking for are:

after that, you can contorl your outlet via the Web-Gui on http://[IP-ADDRESS]:[PORT]/, e.g. http://192.168.1.10:80 (or port 5001 as per default).
via shell, you can use pilight-send as described here: https://wiki.pilight.org/doku.php/psend

Hint:
pilight isn’t restricted to 433MHz power outlets, as you can see in the protocol-list, there many more devices like weatherstations, motion sensors, presence detectors, … basically every 433 MHz device should be controllable via pilight.

hey guys,

thank you for your answers here, especially Florian.
Now there is one question left for my part. Is there a possibility to implement the things, items, etc. directly in the jsondb? Or do I need to use the separate files? And whats the best way to edit the jsondb file? I made the experience that the jsondb content manually added is deleted after a reboot of OH2.

Best regards,
Max

Great tutorial.But I never got it to work. It wouldn’t let my user “openhabian” execute the command. I tried with raspberry-remote and wiringpi. wiringpi stated an error message like “Unable to open /dev/mem or /dev/gpiomem…” I tried several solutions with adding more rights to that openhabian user but without success. And I haven’t been able to run the command in openhab as sudo.

Update: Solved with chmod 666 /dev/gpiomem. Works great!

Hi guys,

is it possible to configure a simple on-off-switch with the exec command in the Paper UI?
I was able to connect a switch item to the “running” channel of a Thing based on the exec binding but the Switch cannot be toggled. I am not sure how I can set-up different states ( ON OFF ) in the command field within the Paper UI to get the switch to work.

Thanks and best regards,
Martin

@SnitzelWeck92 I’m trying to replicate your setup but having issues with one error in my openhab.log. Did you come across this issue during your setup? The exec command and accompanying script which turns the light on and off fails to execute.

2017-03-30 23:30:06.166 [WARN ] [ome.core.thing.internal.ThingManager] - Cannot delegate command 'ON' for item 'PowerSwitch1Switch' to handler for channel 'exec:command:PowerSwitch1Switch-control:input', because no thing with the UID 'exec:command:PowerSwitch1Switch-control' could be found.

thx all

my config is as follows:

.sitemap
Switch item=elroexe_1 label=“Fluter” mappings=[ “on”=“ON”, “off”=“OFF” ]
Switch item=elroexe_2 label=“TV Backlight” mappings=[ “on”=“ON”, “off”=“OFF” ]

.items
String elroexe_1 (gPilight) { channel=“exec:command:elro1:input”}
String elroexe_2 (gPilight) { channel=“exec:command:elro2:input”}

.things
Thing exec:command:elro1 [ command="/usr/local/bin/pilight-control -d elrostecker1 -s %2$s", interval=0, timeout=5, autorun=true ]
Thing exec:command:elro2 [ command="/usr/local/bin/pilight-control -d elrostecker2 -s %2$s", interval=0, timeout=5, autorun=true ]

cheers opa

1 Like

How do the status things work?
I can’t see any return in the .sh file.

Hy. I hope I find help here. I’ve been trying to use a 433 mhz module for a long time with openhab.
I can use it in the shell with the following command: /home/openhabian/433Utils/RPi_utils/./codesend 13108705.

And now I try desperately to integrate this command with exec binding (version 2). I hope who can help, I try to send the command as a push button.
Where is my mistake? Post my composition. Thank you

.items

Group Licht_EG "Licht" 

Switch Wohnzimmer_Decke (Licht_EG) [ "Switchable" ] { channel="exec:command:Wohnzimmer_Decke:run", autoupdate="false" }

.things

//Wohnzimmer_Decke
Thing exec:command:Wohnzimmer_Decke [ command="/home/openhabian/433Utils/RPi_utils/Wohnzimmer_decke.sh %2$s, interval=0, autorun=true ]

.sitemap

sitemap Schneeberggasse16 label="Erdgeschoss" {
    Frame label="Licht" {
        Switch item=Wohnzimmer_Decke mappings=[ "ON"="ON", "OFF"="OFF" ] icon="light"
        Switch item=Wohnzimmer_Led mappings=[ "ON"="ON", "OFF"="OFF" ]
        Switch item=steckdoseG3Switch mappings=[ "ON"="ON", "OFF"="OFF" ]
        Switch item=steckdoseG4Switch mappings=[ "ON"="ON", "OFF"="OFF" ]
        
        Switch item=Licht mappings=[ "ON"="ON", "OFF"="OFF" ]
        
        Switch item=Dash_Switch
        }
}

.sh

#!/bin/sh


if [ "$1" = "off" ] || [ "$1" = "0" ] || [ "$1" = "OFF"  ]; then
        /home/openhabian/433Utils/RPi_utils/./codesend 13108705
else
        /home/openhabian/433Utils/RPi_utils/./codesend 13108705
fi

rule

rule "Poweroutlet B"
  when
    Item Wohnzimmer_Decke received command
  then
   if(receivedCommand == ON){
        Wohnzimmer_Decke.sendCommand("13108705")
     }else{
       Remote_Send_Args.sendCommand("10010 2 0")
     }

      // wait for the command to complete, state will be NULL if not used befor$
      while(Remote_Send.state != OFF){
         Thread::sleep(500)
      }
      logInfo("Power_Plug", "Resuts are: \n" + Remote_Send_Out.state )
end

The Rule is from a other tutorial.

OK, I am - damn - I am stuck.
I use the latest OPENHABIAN (OPENHAB 2.2) version on my RASPBERRY PI.

I just want to do a simple on - off switch with a stupid INTERTECHNO PAR1500 switch using my ITGW433.

To switch it on, the command is:

/usr/bin/python /etc/openhab2/senditgw433.py A 1 on
or
/usr/bin/python /etc/openhab2/senditgw433.py A 1 off

to switch it off.

What I did is:

SITEMAPS:
sitemap zuhause label=“Zuhause” {
Frame label=“Intertechno” {
Switch item=PAR1500_A1 label=“Bunte Lichterkette” mappings=[ “on”=“on”, “off”=“off” ]
}
}

THINGS:
Thing exec:command:PAR1500_A1 [command="/usr/bin/python /etc/openhab2/senditgw433.py A 1 %2$s"]

ITEMS:
String PAR1500_A1 {channel=“exec:command:PAR1500_A1:input”}

I get this error from the log-file:
[hab.binding.exec.handler.ExecHandler] - An exception occurred while formatting the command line with the current time and input values : ‘Format specifier ‘%2$s’’

What is wrong?

Thank you !!!
Peter

Have a look here for a detailed setup with the exec binding

I don’t know if it is possible to directly set on/off from the mapping to the format specifier. Maybe there is no way around a rule.

But I had read that this error only occurs once when the item is not defined, after first execute it should be gone. If not try the setup like in the Tutorial.

Thank you Josar. That helped a lot. No more error messages in the log file.

But still, it doesn‘t work. :neutral_face:
I can change the switch and the command „ON“ (or „OFF“) is given. See Log file.
But it feels like EXEC does not execute anything…

Logfile now is:

Item 'PAR1500_A1' received command ON
PAR1500_A1 changed from NULL to ON

Sitemaps:

sitemap zuhause label="Zuhause"
{     
   Frame label="Intertechno" {
        Switch item=PAR1500_A1 label="Steckdose A1"
        }
 }

Things:

Thing exec:command:remote-send [
      command="/usr/bin/python /etc/openhab2/senditgw433.py %2$s",
      interval=0,
      autorun=true
       ]

Items:

Switch PAR1500_A1 <poweroutlet>
Switch Remote_Send { channel="exec:command:remote-send:run" }
String Remote_Send_Args { channel="exec:command:remote-send:input" }
String Remote_Send_Out { channel="exec:command:remote-send:output" }

Rules:

rule "itgw"
  when
    Item PAR1500_A1 received command
  then
    if (receivedCommand == ON) {
      Remote_Send_Args.sendCommand("A 1 on")
      }
    else {
      Remote_Send_Args.sendCommand("A 1 off")
      }
     while(Remote_Send.state != OFF) {
       Thread::sleep(500)
      }
      logInfo("Intertechno A1", "Results are: \n" + Remote_Send_Out.state)
end

@toaopeter

This should produce an output in the log when the rule is fired.I cannot see this in the log you posted.
So either the rule is not fired, rebooting Pi could help. Or you did not post the line.

  1. Are you able to execute the command as user openhabian in the command line?
sudo -u openhabian /usr/bin/python /etc/openhab2/senditgw433.py A 1 on
  1. are you able to execute the rule? Is your rule file called .rules ? This is randome guessing why your rule is not fireing. try some simple rule.
rule "itgw"
  when
    Item PAR1500_A1 received command ON
  then
     // Set button back to see if rule fires
    PAR1500_A1.postUpdate(OFF)
end

It is solved!

To answer your first question: Yes, I can execute with the user „openhabian“.

Question 2: Yes, my rules have the ending .rules

Your simplified rule worked well. So far so good.

But I also had some trouble with this:

[hab.binding.exec.handler.ExecHandler] - Couldn’t transform response because transformationService of type ‘REGEX’ is unavailable

This was solved somewhere else and I managed to fix it.
I forgot to install the EXEC transformation service. Well…

Now your script works.

But I found out that I can‘t just copy & paste this code. It just doesn‘t work to use multiple “when“ commands in one .rules file. At least I don’t know the syntax to do it right.

So I split it into three .rules files and now I can switch all three powerplugs on and off.
Very well. It might me not the most smart solution to use three .rules files but it works.

Thank you again to help me climb over these last borders…

PS: I reformatted my ugly post

@toaopeter this is explicitly stated in the tutorial!

You may have multiple rules in one rulefile, do not forget to change the name of the rule when copy pasting or multiple trigger in one rule.

rule "First Rule"
  when
    <TRIGGER CONDITION1>
  then
     // Do stuff
end

rule "Second Rule"
  when
    <TRIGGER CONDITION2>
  then
     // Do stuff
end
when
    <TRIGGER CONDITION1> or
    <TRIGGER_CONDITION2> or
    <TRIGGER_CONDITION3>
    ...
then

https://docs.openhab.org/configuration/rules-dsl.html

I think your Item needs to be of type String, not Switch

@tntdynamight Nope, this is all right. The run channel is a switch.

Hi All,

is there a way to detect an item is on/off using PING but using the exec binding ? The state of this item will be used in another rule

as the network binding is not capable of doing what I require, im thinking I can executecommandline a ping, but checking if it replies or not is what im struggling with

Thanks!