Trigger Sonoff devices using SHAC

Discussion in 'C-Bus Automation Controllers' started by philthedill, Aug 30, 2020.

  1. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    Has anyone had any success on triggering Sonoff devices from SHAC. They are a very cost effective means of adding to an existing C-Bus system either using switched plugs, light relays or the 4 channel din rail mounted relay?
     
    philthedill, Aug 30, 2020
    #1
  2. philthedill

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    Have you flashed the firmware to Tasmota (or similar) so that MQTT is available? Using MQTT gives you the best flexibility for HA devices like this and you also get Alexa control at the same time.
     
    znelbok, Aug 31, 2020
    #2
  3. philthedill

    chromus

    Joined:
    Jan 27, 2014
    Messages:
    422
    Likes Received:
    50
    Location:
    Perth
    The easiest way I've found is CBUS homebridge, it has a sonoff plugin and "just works" once configured.

    HOWEVER,
    I walked away from the client after researching the product further.

    Sonoff as a product is woeful for quality assurance and many of their appliances don't meet Au electrical standard so beware if you are are in a commercial setting, don't take on the nightmare that could happen due to faulty 240V gear.
     
    chromus, Aug 31, 2020
    #3
    Ashley likes this.
  4. philthedill

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,522
    Likes Received:
    173
    Location:
    Adelaide, Australia
    This has been my experience with Sonoff devices. While Znelbok has the best solution, after I experimented with these for a while I decided they just weren't up to a suitable standard.
     
    Ashley, Aug 31, 2020
    #4
    Damaxx likes this.
  5. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    that is sounding beyond my skill set. If possible, I'd prefer NOT to flash the Sonoff devices
     
    philthedill, Sep 1, 2020
    #5
  6. philthedill

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    Find someone who can as it rally makes the so much more usable.

    The other option is to find one that can be flashed over the air (Bunnings were selling some with a USB port as well). Tasmota is a wonderful implementation for these plugs.

    Have a look here for everything in AU that can be flashed with Tasmota (and thus MQTT control)

    https://templates.blakadder.com/au.html
     
    znelbok, Sep 1, 2020
    #6
  7. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    Hey there znelbok, there is an excellent you-tube on Tasmota flashing so I will be doing that and can see why you suggested it. If that is out of the way, how do I send command from my SHAC to the modified sonoff? simple language please....
     
    philthedill, Sep 2, 2020
    #7
  8. philthedill

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    That I can;t answer as I got my SHAC last night and I am learning about it right now.

    In summary though - there is a MQTT LUA "plugin" (I'm here right now to look for it) that has been covered in a thread somewhere.

    Once you have a MQTT broker setup you just publish commands to the desired topic (you define this in tasmota for the plug) and it will respond. Similary, the SHAC will also subscribe to topics and when the value changes it should be able to do something (like turn on a C-bus group).

    Others with experience will chip in and correct where I am wrong, but give me a day or two and I should know a lot more "details"
     
    znelbok, Sep 2, 2020
    #8
  9. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    I've now received the Sonoff device and have installed Tasmota on it. Just wondering if you made any more progress on what the next steps might be to enable control via MQTT from the SHAC? with thanks
     
    philthedill, Sep 24, 2020
    #9
  10. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    It looks easier to use http post to get the Tasmota flashed sonoff device. Can anyone see what is wrong with this bit of code? Thanks

    local http = require("socket.http")
    if
    GetCBusLevel(0, 56, 183) == 255
    then
    http://"192.168.86.79"/cm?cmnd=Power1%20On -- this is line 8
    else
    http://"192.168.86.79"/cm?cmnd=Power1%20off
    end

    I am getting this error message .....
    Lua syntax error at line 8: '<name>' expected near '/'
     
    philthedill, Sep 25, 2020
    #10
  11. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    you need to call http.request() and specify url as a string. also i noticed your on command has capital O but off has lower case o?

    Code:
    function send_http_get(ip, url)
       http = require('socket.http')
     
       res, err = http.request({
          url = 'http://' .. ip .. url,
        method = 'GET',
      })
    end
    
    if GetCBusLevel(0, 56, 183) == 255 then
      send_http_get('192.168.86.79/', 'cm?cmnd=Power1%20On')
     else
      send_http_get('192.168.86.79/', 'cm?cmnd=Power1%20off')
    end
     
    Pie Boy, Sep 25, 2020
    #11
  12. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    thank you - that works fine
    apparently no issue with the O and o
    summary - I now have a Tasmota flashed Sonoff device working directly from SHAC without any intermediary!
     
    philthedill, Sep 25, 2020
    #12
  13. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    The next challenge is how to get some feedback happening. ie: can the Tasmota device send something back to the SHAC to indicate it's state? This could then be extended to add push buttons and sensors to the core C-Bus system using wi-fi at low cost.....
     
    philthedill, Sep 26, 2020
    #13
  14. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    you can query the sonoff device status with '?m' i think or in your case it might be 'cm?' or '?cm' and to retrieve the result of the http get command see below, then we would need to process the reply into something usable, like return 2 args fro the function one for each output/input, how many outputs/inputs dose your sonoff device have? and what dose log(reply) print to the log? If you want 2way communication and up to date feedback most likely MQTT is the way to go

    Code:
    function send_http_get(ip, url)
       http = require('socket.http')
       ltn12 = require('ltn12')
      sink = {}
       res, err = http.request({
          url = 'http://' .. ip .. url,
          method = 'GET',
          sink = ltn12.sink.table(sink)
      })
    
      if sink then
        return table.concat(sink)
      else
        return nil, err
      end
    end
    
    reply = send_http_get('192.168.86.79/', '?m')
    log(reply)
    
     
    Last edited: Sep 27, 2020
    Pie Boy, Sep 27, 2020
    #14
  15. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    Hi Pie Boy - on the right track here I think. My device is a 4 channel switch flashed with Tasmota. This is the response written to the log file using the script you provided this morning
    < * string: {t}{s}DHT11 Temperature{m}null °C{e}{s}DHT11 Humidity{m}null %{e}{s}DHT11 Dew point{m}null °C{e}</table>{t}<tr><td style='width:25{c}normal;font-size:38px'>OFF</div></td><td style='width:25{c}bold;font-size:38px'>ON</div></td><td style='width:25{c}normal;font-size:38px'>OFF</div></td><td style='width:25{c}normal;font-size:38px'>OFF</div></td></tr></table> >
     
    philthedill, Sep 28, 2020
    #15
  16. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    under log(reply) try this
    Code:
    data = {}
         for val in string.gmatch(reply, ">(%a+)<") do
            table.insert(data, val)
         end
    
    log(data)
    
    this should give you something like the below in the log
    * table:
    [1]
    * string: OFF
    [2]
    * string: ON
    [3]
    * string: OFF
    [4]
    * string: OFF

    Then you can check what each value is, then do something...
    Code:
    if data[1] == "ON" then
    
    end
    
    if data[1] == "OFF" then 
    
    end
    
    But this wont really work for an input as such you would need to constantly send the above command to be up to date.
    dose your switch also have outputs? if so the above would work to query the status etc
     
    Pie Boy, Sep 28, 2020
    #16
  17. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    here is what I got back...

    test query of Tasmota state 28.09.2020 15:30:17
    * table:
    [1]
    * string: OFF
    [2]
    * string: OFF
    [3]
    * string: OFF
    [4]
    * string: OFF

    I had already concluded that this would need to a "resident" script and therefore not really perfect. Re your question re "outputs", I expect the device has something as it is compatible with various middlemen like MQTT. but I cannot see how it might push a change of state?
     
    philthedill, Sep 28, 2020
    #17
  18. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    I would keep an eye on CPU usage if you plan
    To use a resident script for this,
    things like this can consume cpu power and cause issues.

    Usually when a device connects to an mqtt broker all events/topics from that device are sent to the mqtt message bus and can be read by any device subscribed to those topic/s

    That is how a state change would be sent, the shac would then need to connect to the same mqtt broker and subscribe to the topic and thus receive the event from the switch Etc
     
    Pie Boy, Sep 28, 2020
    #18
  19. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    140
    Likes Received:
    3
    Location:
    Melbourne
    Agree re avoiding resident script it the way to go for the reason you state. IF an MQTT broker can send a topic update to the SHAC (which your statement implies). Do you know how it gets pushed into the SHAC?
     
    philthedill, Sep 29, 2020
    #19
  20. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    248
    Likes Received:
    31
    Location:
    New Zealand
    Pie Boy, Sep 29, 2020
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.