Hass.io Add-on: Sonoff Server / Sonoff no need of flash

Hi,

First, thank you for this post and what you created, it’s very useful even if it’s been quite a challenge for me.

So, I followed the instructions on the GitHub page, and I successfully installed Node Red, and the node-red-contrib-sonoff-server. So far so good, even if I struggled quite a bit.

One thing I can’t figure out though: How to access the sonoff server on :1080. I’ve configured it in node red and it seems to be working since I have:

Aug 22:41:29 - [info] [sonoff-server:xxxxxxxx.xxxxxx] SONOFF Server Started On Port 1080

However, when I try to access the server using my duckdns url (https://myserver.duckdns.org:1080), i get an empty response. On 1081, I get access refused.

Both port are forwarded to my pi’s address using TCP (not sure of the terminology here, I’m just a simple hobbyist).

Questions:

  • Even if I haven’t got to the post request part with the sonoff, I should be able to access the server interface, right ?
  • How to properly configure the sonoff server in node red ? I have no clue what the websocket value should be.

you don’t need node-red

/custom_components/light/sonoff.py

I’d just use the hassio sonoff server referred to in this thread. It works really well and there is a nice little UI to setup your devices.

You do need to be able to post the commands to your sonoff though. If you use the exact details with postman, as mentioned above you should be golden. Ensure you have your sonoff device in paring mode and you have connected to it as wifi access point, and the ip is 10.10.7.1 as mentioned above.

1 Like

I’m getting the following error now, I can confirm I have a sonoff.devices.json and a sonoff.config.json.

Error while setting up platform sonoff
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 129, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/light/sonoff.py", line 43, in setup_platform
    with open('/config/sonoff.ha.json') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/config/sonoff.ha.json'

Should I assume this is meant to be ‘/config/sonoff.devices.json’ in sonoff.py?

create a sonoff.ha.json in your config directory with the content of http://hassio.local:1080/hadevices

i will fix many issues soon

great thanks will give it a try

Error while setting up platform sonoff
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 129, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/light/sonoff.py", line 44, in setup_platform
    for device in json.loads(f.read()):
  File "/usr/local/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.6/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 9 (char 16)

I guess I’ll need to tweak the json from that endpoint a little…

tomorrow i will publish a new update.

1 Like

Thanks for your feedback !

I’m at the exact same point as you appear to be.
However, I still can’t see the sonoff on the :1080 interface. i’m using duckdns, so I guess I should put the address, without the port, under serverName in the post request, right ?

edit: After extensive research, i found this:

  1. The LED indicator quickly blinks twice and repeats, which means the device has connected to the router but fail to connect to the server.

Which is exactly what I have. So I guess it must be due to the fact that I’m using duckdns or something like that, my request must be messed up. Here’s what I’ve tried so far, maybe one of you guys will spot something obvious:

{“version”:4,“ssid”:“MySSID”,“password”:“MyPassword”,“serverName”:“https://REDACTED.duckdns.org”,“port”:1081}

Use your internal home assistant IP address. I’d love to be able to access the interface via HTTPS so I can make a panel quick link to it, but can’t see how to get it to serve as HTTPS. I noticed there is an SSL port of 1081, but that didn’t seem to work either.

Looking forward to the update, it seems so close! This is a really amazing feature for home assistant, I could see it being an integration at some point, like the Sonos or hue integrations.

Got it. This didn’t solve the issue but I’ll use the internal IP from now on. The port is 1081, as in your example, right ?

I’m wondering because I can access the server using 1080.

Currently, I have the same error message as you do, hopefully the update will solve this issue.

Hi @gerswin

Just wondered if there is any update on your changes? Also I think I’ve got the custom component in and I can control my sonoff using the server directly, but I’m not sure what the naming convention of the entities should be in home assistant and how to go about using the custom component to use lights within a group for example?

Thanks in advance

Andy

EDIT:

Ok so I formatting the Json from the /hadevices endpoint, it needed quotes around the field names to work:

[
    {
        "name": "Back Door Light",
        "service": "Lightbulb",
        "type": "",
        "outlet": 0,
        "state": false,
        "device": "1234567890",
        "uid": "abc1234gh",
        "intID": 2
    }
]

After this, I restarted and could see my devices with the names I gave them. I no longer get errors and can control my devices. Would just be great to get some of the niggles sorted but other than that it’s great.

Hi @mcinnes01,

A few questions if I may:

  • I’m stuck with an error similar to what you previously got:

Error while setting up platform sonoff
Traceback (most recent call last):
File “/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py”, line 129, in _async_setup_platform
SLOW_SETUP_MAX_WAIT, loop=hass.loop)
File “/usr/local/lib/python3.6/asyncio/tasks.py”, line 358, in wait_for
return fut.result()
File “/usr/local/lib/python3.6/concurrent/futures/thread.py”, line 56, in run
result = self.fn(*self.args, **self.kwargs)
File “/config/custom_components/light/sonoff.py”, line 44, in setup_platform
for device in json.loads(f.read()):
File “/usr/local/lib/python3.6/json/init.py”, line 354, in loads
return _default_decoder.decode(s)
File “/usr/local/lib/python3.6/json/decoder.py”, line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/local/lib/python3.6/json/decoder.py”, line 357, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

How did you overcame this ? Did you edit /config/sonoff.devices.json ?

Thanks for any help you can provide.

The error is telling you that the JSON is invalid.

raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

You need to ensure the JSON you paste into sonoff.ha.json looks exactly like my example. so you need quotes around all the JSON field names:

[
    {
        "name": "Back Door Light",
        "service": "Lightbulb",
        "type": "",
        "outlet": 0,
        "state": false,
        "device": "1234567890",
        "uid": "abc1234gh",
        "intID": 2
    }
]

Give HA a restart afterwards and check the logs, you should only see one warning about the custom component then, and no errors.

@mcinnes01 Ok got it, the explanation is clear, thank you.
One last thing because I’m dumb: How do you get device, uid and other info ? I guess I should define the name myself but for the others, I’m not sure.

It’s the JSON you get from the sonoff server endpoint. You need to go to the server http://hassio.local:1080, then you should see your device id in the drop down. Select it and configure the channels pressing save.

After that if you click the link on the left to devices, you should see your device’s channels with green button you can use to toggle the state.

Then follow this instruction to get he device JSON that goes in your sonoff.ha.json which contains all the uid’s for your device(s):

gerswin

3d

create a sonoff.ha.json in your config directory with the content of http://hassio.local:1080/hadevices 1

i will fix many issues soon

Then you need to make sure you fixup the JSON formatting as described above.

I also noticed a bug, you can set the service e.g. lightbulb, switch, etc when you add your device channels, but it only seems to save one if you have multiple channels, I think it’s likely a UI bug. So if you look at my JSON I set the service on them all. No idea what the type field is for.

That’s my issue, the dropdown is empty. There must be something wrong with my post request I guess, because everytime I configure my sonoff (=fast blinking led after pressing the button), it goes back to blinking twice once I send the post request.

This means the device is connected to hte router, but can’t reach the sonoff server.

Damn I really can’t figure out that one :confused:

i’m testing the fix! :slight_smile:

done, is fixed.

just config you sonoff devices and sonoff.ha.json will create automatically