OH3: Sonoff DIY mode with Rest API & HTTP Binding

I am trying to set up Sonoff DIY with rest api & http binding

Can someone tell me why the state(switch: on/off) of the device(channel id: switch_trest) is not updated when i use external switch?

I use almost same configuration in channel - id: stateonly and state(switch: on/off) gets updated when i use external switch.
I dont understand why this is happening?


UID: http:url:66be259eda
label: Test 3
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://192.168.100.45:8081/zeroconf/
  delay: 500
  stateMethod: POST
  refresh: 6
  commandMethod: POST
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: switch_trest
    channelTypeUID: http:switch
    label: switch_trest
    description: ""
    configuration:
      onValue: '{"deviceid":"","data":{"switch":"on"}}'
      stateContent: '{"deviceid":"","data":{}}'
      stateExtension: info
      offValue: '{"deviceid":"","data":{"switch":"off"}}'
      commandExtension: switch
      stateTransformation: JSONPATH:$.data.switch
  - id: stateonly
    channelTypeUID: http:string
    label: stateonly
    description: ""
    configuration:
      mode: READONLY
      stateContent: '{"deviceid":"","data":{}}'
      stateExtension: info
      stateTransformation: JSONPATH:$.data.switch

Thank you in advance

You might find clues in your events.log and/or openhab.log

Does your device really send an empty deviceid field?
Anyway, you appear to have set up to use JSONPATH to extract on/off, then try to match on/off with a longer string of JSON, which isn’t going to match.

1 Like

Hi,
Thanks for your reply.
No my device does return deviceid field but i didnt use it since it worked without it.
Since i dont have much experience on this i did not understand the second part with “then try to match on/off with a longer string of JSON, which isn’t going to match.” :slight_smile:
I will read a bit then try to fix this.
I am confused because state of “Item ‘Test3_stateonly’” is correct when i press external switch and configuration is the same.

Logs:

2021-08-16 16:18:19.026 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Test3_switchtrest' changed from OFF to UNDEF
2021-08-16 16:18:19.524 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Test3_stateonly' changed from on to off
2021-08-16 16:18:20.796 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Test3_switchtrest' received command ON
2021-08-16 16:18:20.801 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'Test3_switchtrest' predicted to become ON
2021-08-16 16:18:20.812 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Test3_switchtrest' changed from UNDEF to ON
2021-08-16 16:18:25.170 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Test3_switchtrest' changed from ON to UNDEF
2021-08-16 16:18:25.525 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Test3_stateonly' changed from off to on
2021-08-16 16:18:37.532 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Test3_stateonly' changed from on to off
2021-08-16 16:18:43.705 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Test3_stateonly' changed from off to on

Look at your onValue and offValue settings. Remember that you have already used JSONPATH to extract “on” or “off”.

1 Like

Hi,

Thanks for your reply.
I don’t get it :smiley: , as i thought it is, is that these values(on, off value) are for turning device on and off, they have nothing to do with the state of the device. But i guess i misunderstood all this.

onValue, offValue are bi-directional.
An outgoing ON/OFF command is transformed to the given string.
An incoming string that matches one is transformed to ON/OFF state update. No match, no work.

1 Like

I understand now that ON/OFF values should be transformed in other way, or somehow to match those. I will need some example how to do it because i cannot figure this out.
Thanks a lot for you explanation

You already have JSONPATH extracting text on or off from incoming data. Set onValue to on and offValue to off.

1 Like

Hi @rossko57,

Thanks a lot you for your help. Working example below
Solution was to change as you said onValue to on and offValue to off.
Also to create a Transform .map(in this case filename “sonoff.map”) file with

on={"deviceid":"","data":{"switch":"on"}}
off={"deviceid":"","data":{"switch":"off"}}
UID: http:url:66be259eda
label: Test 3
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://192.168.100.45:8081/zeroconf/
  delay: 500
  stateMethod: POST
  refresh: 6
  commandMethod: POST
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: switch_trest
    channelTypeUID: http:switch
    label: switch_trest
    description: ""
    configuration:
      onValue: on
      stateContent: '{"deviceid":"","data":{}}'
      commandTransformation: MAP:sonoff.map
      offValue: off
      stateExtension: info
      commandExtension: switch
      stateTransformation: JSONPATH:$.data.switch
  - id: sonwifi
    channelTypeUID: http:string
    label: wifi
    description: ""
    configuration:
      stateExtension: info
      stateTransformation: JSONPATH:$.data.signalStrength
      stateContent: '{"deviceid":"","data":{}}'