Extract temperature/humidity via Tasmota flashed Sonoff zigbee bridge

Discussion in 'C-Bus Automation Controllers' started by philthedill, Mar 16, 2021.

  1. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    Hi there, I've bought a number of Sonoff (eWeLink) temperature/humidity sensors and have them connected to my SHAC via a Tasmota flashed zigbee bridge and MQTT broker running on Pi3. Everything looks fine but I cannot bridge the last piece and get the temperature/humidity reading together with sensor ID across to a user variable.
    I have this working so far....

    mqtt = require("mosquitto")
    client = mqtt.new()

    client.ON_CONNECT = function()
    client:subscribe("tele/Tasmota_ZBB-1/SENSOR")
    local mid = client:subscribe("tele/Tasmota_ZBB-1/SENSOR", 2)
    end

    client.ON_MESSAGE = function(mid, topic, payload)

    Sensor_message = "return "..payload:gsub('("[^"]-"):','[%1]=')
    output=loadstring(Sensor_message)()
    log(output) --- see below

    end

    broker = "192.168.86.31" -- MQTT broker address - Raspberry Pi
    client:connect(broker)
    client:loop_forever()
    "
    this is what the log is giving me
    Tasmota ZB bridge from MQTT 16.03.2021 13:53:39
    * table:
    [ZbReceived]
    * table:
    [0xF53A]
    * table:
    [LinkQuality]
    * number: 84
    [Device]
    * string: 0xF53A
    [Humidity]
    * number: 61.04
    [Name]
    * string: temp_sensor_1
    [Endpoint]
    * number: 1"
    any clues appreciated..... with thanks
     
    Last edited: Mar 16, 2021
    philthedill, Mar 16, 2021
    #1
    1. Advertisements

  2. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    What’s the raw message from payload var?
    As above location of humidity value would be
    humidity = output.Humidity
    I can’t see the temp value in the above output
     
    Pie Boy, Mar 16, 2021
    #2
    1. Advertisements

  3. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    this is what I can see in the Tasmota console view... will this help

    temperature example:
    16:43:44.184 MQT: tele/Tasmota_ZBB-1/SENSOR = {"ZbReceived":{"0xCB69":{"Device":"0xCB69","Temperature":25.46,"Endpoint":1,"LinkQuality":0}}}

    humidity example:
    16:40:22.355 MQT: tele/Tasmota_ZBB-1/SENSOR = {"ZbReceived":{"0xCB69":{"Device":"0xCB69","Humidity":53.93,"Endpoint":1,"LinkQuality":0}}}


    the devices send either temperature or humidity depending when the variables change by a certain amount - eg 0.1 deg C. I'd like to be able to capture both temp and humidity and update CBus user variables accordingly....
     
    philthedill, Mar 16, 2021
    #3
  4. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    That is possible, are you receiving the MQTT messages on the shac side?

    If you change this bit,
    client.ON_MESSAGE = function(mid, topic, payload)
    log(payload)
    end

    what dose the shac log show?
     
    Pie Boy, Mar 16, 2021
    #4
  5. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    Tasmota ZB bridge from MQTT 16.03.2021 18:15:24
    * string: {"ZbReceived":{"0x9021":{"Device":"0x9021","Humidity":55.21,"Endpoint":1,"LinkQuality":55}}}
     
    philthedill, Mar 16, 2021
    #5
  6. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    Ok, cool try the below

    client.ON_MESSAGE = function(mid, topic, payload)
    require(’json’)

    payload = json.pdecode(payload)
    log(payload)

    if topic = ‘tele/Tasmota_ZBB-1/SENSOR’ then

    if payload[1].Humidity then
    log(payload[1].Humidity)
    —SetUserParam(1,payload[1].Humidity)
    end

    if payload[1].Temperature then
    log(payload[1].Temperature)
    —SetUserParam(2, payload[1].Temperature)
    end

    end

    end
     
    Pie Boy, Mar 16, 2021
    #6
  7. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    that's looking good thank you but I also need to differentiate the sensor name (I have 8 of them). any ideas?
     
    philthedill, Mar 16, 2021
    #7
  8. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    You could differentiate by MQTT topic or by the value Device in the payload I expect that value would be individual to each sensor.

    if payload[1].Device == “0x9021” then

    if payload[1].Humidity then
    log(payload[1].Humidity)
    —SetUserParam(1,payload[1].Humidity)
    end

    if payload[1].Temperature then
    log(payload[1].Temperature)
    —SetUserParam(2, payload[1].Temperature)
    end


    end
     
    Pie Boy, Mar 16, 2021
    #8
  9. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    correct "Device" is unique by sensor
    However, I am having trouble getting it to work....
    what is the "1" referring to in the line <if payload[1].Device == “0x9021” then>?
     
    philthedill, Mar 17, 2021
    #9
  10. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    looks like the index [1] is not working in the lua table because table is indexed by key, try like this, where device value goes as string inside the square brackets

    if payload.ZbReceived['0x9021'] then

    if payload.ZbReceived['0x9021'].Humidity then
    log(payload.ZbReceived['0x9021'].Humidity)
    --SetUserParam(1,payload.ZbReceived['0x9021'].Humidity)
    end

    if payload.ZbReceived['0x9021'].Temperature then
    log(payload.ZbReceived['0x9021'].Temperature)
    --SetUserParam(2, payload.ZbReceived['0x9021'].Temperature)
    end


    end
     
    Pie Boy, Mar 17, 2021
    #10
  11. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    good progress here - thank you. I have the relevant device being recognised but am unable to write it to user parameter. i think I have copied your framework correct but here is a sample and the resultant error message

    elseif
    payload.ZbReceived["0xA315"] then
    log("Device = 0xA315")
    if payload.ZbReceived["0xA315"].Temperature then
    SetUserParam(32, payload.ZbReceived["0xA315"].Temperature)
    elseif payload.ZbReceived["0xA315"].Humidity then
    SetUserParam(42, payload.ZbReceived["0xA315"].Humidity)
    end

    Tasmota ZB bridge from MQTT 18.03.2021 10:54:18
    Library cbuslogic:0: Group number must be integral
    stack traceback:
    [C]: in function 'error'
    Library cbuslogic: in function 'CBusLookupTags'
    Library cbuslogic: in function 'SetUserParam'
    Resident script:47: in function <Resident script:16>
    [C]: in function 'loop_forever'

    ... nearly there!
     
    philthedill, Mar 18, 2021
    #11
  12. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    It appears I had the network number missing from the "SetUserParam" instruction. Appears to be working fine and thanks again to Pie Boy....
     
    philthedill, Mar 18, 2021
    #12
    1. Advertisements

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.