Help creating my first addon?

So, I just switched to hass.io today and realized I couldn’t run my old docker containers… not wanting to revert back to my old outdated AIO install I rather try and make it into a addon since it would help others to!

Oh yeah, good point from @DavidFW1960 below, I’m blaming it on the fact that I’m writing it at 1 in the morning :smiley: The docker container that I want to port to a addon is a decoder for ikea sparsnas energy meter. It uses a dongle to catch the transmission, decodes it and adds it to a mqtt message!

This is the instructions that I’ve used before to install it: https://github.com/tubalainen/sparsnas_decoder/blob/master/docker.md and that’s easy enough.

I’ve read the original tutorial and a guide from danblog but honestly I’m just as lost as I was before!

So far (and I’m just guessing here) I think the config.json should atleast include this;

  "options": {
    "SENSORS": 1234 1000,
    "mqtt_host": 192.168.x.x,
    "mqtt_port": 1883,
    "mqtt_username": "username",
    "mqtt_password": "password"
  },
  "schema": {
    "SENSORS": "int",
    "mqtt_host": "int",
    "mqtt_port": "int",
    "mqtt_username": "str",
    "mqtt_password": "str"
  },

But as I said… that’s just me guessing and I have no clue what to do next if even this is the correct way! :smiley: Would love if someone could point me in the right direction!

I have no idea but there is already a MQTT broker addon with Hassio - why not just use that?

yeah it’s not a mqtt broker, it’s a decoder for IKEA sparsnas energy meter. It just uses the mqtt protocol to send the decoded data as a json to hass!

Ok I wen’t ahead and I’m trying my way forward…
Can anyone tell me what’s wrong with this config.json, hass gives me a error saying WARNING (MainThread) [hassio.addons.data] Can't read /data/addons/local/sparsnas_decode/config.json
I’m guessing it’s the mqtt_host parameter since the second . is getting red marked in Atom but I’ve tried most other types for it but none work!

config.json

{
  "name": "Sparsnas Decoder",
  "version": "0.2",
  "slug": "Decoder for IKEA Sparsnas",
  "description": "Sparsnas Decoder",
  "url": "https://github.com/tubalainen/sparsnas_decoder",
  "startup": "application",
  "boot": "auto",
  "options": {
    "SENSORS": 1234,
    "PULSES": 10000,
    "mqtt_host": 192.168.1.1,
    "mqtt_port": 1883,
    "mqtt_username": "username",
    "mqtt_password": "password"
  },
  "schema": {
    "SENSORS": "int",
    "PULSES": "int",
    "mqtt_host": "float",
    "mqtt_port": "int",
    "mqtt_username": "str",
    "mqtt_password": "str"
  },
}

Been talking with the original developer and now my config.json looks like this:

{
  "name": "Sparsnäs Decoder",
  "version": "0.3",
  "slug": "sparsnas_decoder",
  "description": "Decoder for IKEA Sparsnäs",
  "url": "https://github.com/tubalainen/sparsnas_decoder",
  "startup": "application",
  "boot": "auto",
  "map": ["config:rw"],
  "options": {
    "run command": "docker build -t sparsnas --build-arg SENSORS=1234 --build-arg MQTT_HOST=192.168.x.x --build-arg MQTT_PORT=1883 --build-arg MQTT_USERNAME=username --build-arg MQTT_PASSWORD=password https://github.com/tubalainen/sparsnas_decoder.git",
    "host_network": true,
    "devices": "/dev/bus/usb:/dev/bus/usb:rw"
  },
  "schema": {
    "run command": "str",
    "host_network": "bool",
    "devices": "str"
  }
}

And it should work… but for some reason it seems that hassio just ignores the arguments passed on in the run command since I just keep getting the error: ERROR (SyncWorker_19) [hassio.docker.addon] Can't build local/armhf-addon-sparsnas_decoder:0.3: The command '/bin/sh -c : "${SENSORS:?Build argument 'SENSORS' needs to be set and non-empty.}"' returned a non-zero code: 2

repo is here if anyone wanna give it a look; https://github.com/Naesstrom/sparsnas_decoder

Any updates on this add-on?
This add-on, if made to support amd64 (I have not checked if it already does) would be the last add-on I need to get rid of my Raspberry Pi 2 used only for Deconz and Sparsnäs.

Did you ever solve this? :slight_smile:

No sorry guys I’m not a coder so I never managed to get this working… was hoping that someone might pick it up and continue but no one have yet!

I’ll might look into it when I have time. I’ll get back to you if I have a working solution =)

It doesn’t help you make an add-on, but you can install any regular container using the Portainer add-on.

I can’t find that one on Docker hub and I wasn’t able to build it from Portainer. However, I was able to use the community SSH add-on with “protection mode” disabled to do a git pull of the repo and then docker build with those instructions. After that, it was available to deploy from Portainer using the sparsnas:latest image.

I don’t have anything to test it with so it failed to find any sensors, but I do get log output suggesting that it should work.

As for the error, I think you would need to supply a build.json to specify the additional build args rather than pass them as a “run command”.

Could you specify step-by-step on how you did exactly :)?

Try this:

build.json:

{
  "args": {
    "SENSORS": "1234 1000",
    "MQTT_HOST": "192.168.x.x",
    "MQTT_PORT": 1883,
    "MQTT_USERNAME": "username",
    "MQTT_PASSWORD": "password"
  }
}

config.json:

{
  "name": "Sparsnäs Decoder",
  "version": "0.1",
  "slug": "sparsnas_decoder",
  "description": "Decoder for IKEA Sparsnäs",
  "url": "https://github.com/tubalainen/sparsnas_decoder",
  "startup": "application",
  "boot": "auto",
  "map": ["config:rw"],
  "host_network": true,
  "devices": ["/dev/bus/usb:/dev/bus/usb:rwm"],
  "options": {},
  "schema": {}
}

And add this line right after the last RUN in the Dockerfile:

RUN chmod a+x /sparsnas.sh

You have to update the values in build.json before installing the add-on since it only grabs those when the add-on is created. If you need to change them, you’ll need to hit REBUILD. It’s probably possible to get them from the config instead, like normal, but it would require an additional script to read them and set them on the environment.

It seems that the application is running. But for some reason it says that it cant find my sensor with ID: XXXXXX. So I’m not really sure if it has recognized my RTL-SDR dongle and actually does a real scan.
I have another addon which uses my RTL-SDR dongle. That addon works just perfect.

Maybe set the device in config.json to rwm instead of just rw. That’s the main difference I see.

Wayy, I’m progressing.
First I got an error messeage:

Need to find frequencies
timeout: unrecognized option: t
BusyBox v1.30.1 (2019-02-16 10:01:54 UTC) multi-call binary.
Usage: timeout [-s SIG] SECS PROG ARGS
Runs PROG. Sends SIG to it if it is not gone in SECS seconds.

So I went to line 58 in sparsnas.sh and could see that busybox timeout was using a faulty switch.
I guess I got a newer version of busybox so instead of:

busybox timeout -t 45 $RTL_SDR > /tmp/sparsnas.raw

I did:

busybox timeout 45 $RTL_SDR > /tmp/sparsnas.raw

Now It found the RTL-SDR Dongle:

Need to find frequencies
Found 1 device(s):
  0:  Realtek, RTL2838UHIDIR, SN: 00000001
Using device 0: Generic RTL2832U OEM
Found Fitipower FC0012 tuner
Sampling at 1024000 S/s.
Tuned to 868000000 Hz.
Tuner gain set to 19.20 dB.
Reading samples in async mode...
Signal caught, exiting!
User cancel, exiting...
Trying -100000 hz...
Trying -95000 hz...
Trying -90000 hz...
Trying -85000 hz...
Trying -80000 hz...

......

#define FREQUENCIES {XXXXXXXXXXXXXXXXXXXX}
Found 1 device(s):
0: Realtek, RTL2838UHIDIR, SN: 00000001
Using device 0: Generic RTL2832U OEM
Found Fitipower FC0012 tuner
Sampling at 1024000 S/s.
Tuned to 868000000 Hz.
Tuner gain set to 19.20 dB.
Reading samples in async mode…

And its sending to my MQQT Broker:

Thank you so much!!! Much appreciated.
I guess I can create a fork in github and also probably introduce the parameters as variables in the config.json file, so that there is no need to do a rebuild when changing the config. But for now it works great!

Hi guys, I have created a Hass.io Add-On for IKEA Sparsnäs Decoder. Please check it out :)!
https://github.com/asifma/hassio-addons/tree/master/sparsnas_decoder

1 Like

Wow that was fast!
I tried installing it this morning but couldn’t tough:

19-04-01 05:23:23 INFO (MainThread) [hassio.addons.git] Clone add-on https://github.com/asifma/hassio-addons/tree/master/sparsnas_decoder repository
19-04-01 05:23:24 ERROR (MainThread) [hassio.addons.git] Can't clone https://github.com/asifma/hassio-addons/tree/master/sparsnas_decoder repository: Cmd('git') failed due to: exit code(128)
  cmdline: git clone --depth=1 --recursive --shallow-submodules -v https://github.com/asifma/hassio-addons/tree/master/sparsnas_decoder /data/addons/git/8360af88
  stderr: 'Cloning into '/data/addons/git/8360af88'...
fatal: repository 'https://github.com/asifma/hassio-addons/tree/master/sparsnas_decoder/' not found
'.
19-04-01 05:23:24 ERROR (MainThread) [hassio.addons] Can't load from repository https://github.com/asifma/hassio-addons/tree/master/sparsnas_decoder
19-04-01 05:23:25 INFO (MainThread) [hassio.addons] Load add-ons: 68 all - 0 new - 0 remove
19-04-01 05:23:47 INFO (MainThread) [hassio.addons.git] Clone add-on https://github.com/asifma/hassio-addons repository
19-04-01 05:23:48 INFO (MainThread) [hassio.addons] Load add-ons: 69 all - 1 new - 0 remove
19-04-01 05:24:04 INFO (MainThread) [hassio.addons.addon] Create Home Assistant add-on data folder /data/addons/data/c57bd9f6_sparsnas_decoder
19-04-01 05:24:04 INFO (SyncWorker_17) [hassio.docker.addon] Start build c57bd9f6/armv7-addon-sparsnas_decoder:0.1
19-04-01 05:24:11 INFO (MainThread) [hassio.api.proxy] /homeassistant/api/states/device_tracker.sussa_presence access from 1e72b768_better_presence
19-04-01 05:24:34 ERROR (SyncWorker_17) [hassio.docker.addon] Can't build c57bd9f6/armv7-addon-sparsnas_decoder:0.1: The command '/bin/sh -c apk add --no-cache g++ mosquitto-dev &&     g++ -o /build/sparsnas_decode -O2 -Wall /build/sparsnas_decode.cpp -lmosquitto' returned a non-zero code: 2

Did you add this repo url?

I have added a V.02 and did some changes to the Dockerfile. It’s hard to troubleshoot as I cant reproduce the error…lets see if anyone else gets similar errors…