Please kindly suggest, mysensors serial gateway to Home Assistant

I would like to have mysensors 2.0 connected to Home Assistant. I have already setup mysensors, a distance sensor and NRF24L01 serial gateway to work properly. Then I connected the Serial Gateway to one of the USB port of Raspberry PI3. I 've got UART address from dmesg command. Then I have configured Home Assistant in mysensor.yaml as shown below. But it doesn’t work. When I looked into the log file. It says that mysensors2.json File does not exist or is not readable. I searched the whole Raspbian directories It wasn’t there. I have read some threads about pymysensors and pymyserial which I understood very little. So I don’t know what to do next. May I ask you that Do I need to install pymysensors or pymyserial in raspbain before I could proceed with Home Assistant. Which one could be used with Home Assistant. Which one is compatible with Home Assistant 0.30.2 Thank you very much for your kind suggestion.

gateways:

  • device: ‘/dev/ttyAMA0’
    persistence_file: ‘path/mysensors2.json’
    baud_rate: 115200
    debug: true
    optimistic: false
    persistence: true
    retain: true
    version: 2.0

Here is my extracted log file from Home Assistant.

16-10-22 19:03:17 homeassistant.util.package: Attempting install of https://github.com/theolind/pymysensors/archive/8ce98b7fb56f7921a808eb66845ce8b2c455c81e.zip#pymysensors==0.7.1
16-10-22 19:03:33 mysensors.mysensors: File does not exist or is not readable: path/mysensors2.json
16-10-22 19:03:33 mysensors.mysensors: Trying backup file: path/mysensors2.json.bak
16-10-22 19:03:33 mysensors.mysensors: File does not exist or is not readable: path/mysensors2.json.bak
16-10-22 19:03:33 mysensors.mysensors: Failed to load sensors from file: path/mysensors2.json
16-10-22 19:03:33 homeassistant.core: Bus:Handling <Event component_loaded[L]: component=mysensors>
16-10-22 19:03:33 homeassistant.core: Bus:Handling <Event platform_discovered[L]: discovered=, service=load_platform.sensor, platform=mysensors>
16-10-22 19:03:33 homeassistant.core: Bus:Handling <Event platform_discovered[L]: discovered=, service=load_platform.switch, platform=mysensors>
16-10-22 19:03:33 homeassistant.loader: Loaded switch.mysensors from homeassistant.components.switch.mysensors
,

Hello @kiratikarnpat I am having a similar issue in this post:

I’m still troubleshooting it, but @martinhjelmare is very helpful!

For the persistence file, you have to specify the correct path to a directory that the user running hass has access to, and a filename. The file will be created when the first sensor sends a report to home assistant.

path/mysensors2.json is not a valid path.

I recommend really reading the documentation and not only copy pasting the config example.

Activate debug level log for mysensors.mysensors with the logger component and post all mysensors related lines after start. Then I can advice more, if there is a problem somewhere.

Thank you for sharing, shep. Thank you, martinhjelmare for the advice, I wil do as you suggested. I just finished reading shep.'s thread. I could understand something more, Thanks to both of you.

I did as martinhjelmare suggested. It works. I put the right path to persistence_file: ‘/var/opt/homeassistant/mysensors1.json’. It created the file mysensors1.json automatically in that path. and update the distance to show the new value every two seconds as I configured in Mysensors. The sensor.distance_sensor_2_2 also appears in the HA state page. At first It did not work because two causes. The first one is the path to .json file. and second one is the port which I connected to Raspberry PI. I use a terminal command dmesg | grep tty. I got the response as shown below. I found that the right port is /dev/ttyUSB0. It is not dev/ttyAMA0 as I put in the HA configuration earlier. When I change the path to .json file and change to the correct port, it works. Anyway when I look into the HA log file it still reports that File does not exist or is not readable. But I ‘ve got the distance value reading on HA state page already. One thing more after connecting Serial Gateway to Raspberry PI, I have to reboot the sensor so that the sensor could connect to the serial gateway. I hope It will work in shep’ s too. Thanks you, shep and Thanks a lot to you, martinhjelmare.

‘’’’
pi@raspberrypi:~ $ dmesg | grep tty
[ 0.000000] Kernel command line: 8250.nr_uarts=1 dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.board rev=0xa22082 bcm2709.serial=0x745e039b smsc95xx.macaddr=B8:27:EB:5E:03:9B bcm2708_fb.fbswap=1 bcm2709.uart_clock=48000000 vc_mem.mem _base=0x3dc00000 vc_mem.mem_size=0x3f000000 dwc_otg.lpm_enable=0 console=ttyS0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=e xt4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
[ 0.000479] console [tty1] enabled
[ 0.274815] console [ttyS0] disabled
[ 0.274879] 3f215040.uart: ttyS0 at MMIO 0x3f215040 (irq = 59, base_baud = 31250000) is a 16550
[ 0.275402] console [ttyS0] enabled
[ 1.834209] 3f201000.uart: ttyAMA0 at MMIO 0x3f201000 (irq = 87, base_baud = 0) is a PL011 rev2
[ 3.460004] systemd[1]: Expecting device dev-ttyS0.device…
[ 3.508744] systemd[1]: Starting system-serial\x2dgetty.slice.
[ 3.509004] systemd[1]: Created slice system-serial\x2dgetty.slice.
[ 5.806107] usb 1-1.4: ch341-uart converter now attached to ttyUSB0
[46208.201940] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[46479.946343] usb 1-1.4: ch341-uart converter now attached to ttyUSB0
‘’’’
Below is the contents of mysensor1.json file.

‘’’’
{“0”: {“battery_level”: 0, “sketch_version”: null, “children”: {}, “sketch_name”: null, “sensor_id”: 0, “type”: 18, “protocol_version”: “2.0.0”}, “2”: {“battery_level”: 0, “sketch_version”: “1.0”, “children”: {“2”: {“values”: {“13”: “169”}, “description”: “”, “type”: 15, “id”: 2}}, “sketch_name”: “Distance Sensor”, “sensor_id”: 2, “type”: 17, “protocol_version”: “2.0.0”}}
‘’’’

1 Like

The first time you add the sensors to home assistant, you have to have the gateway connected to home assistant before you start the sensors. After that, as long as persistence is activated, you should be able to restart home assistant without having to restart the sensors. The sensors will be loaded using the persistence file.

The warning about the persistence file should not be shown if it exists when restarting home assistant.

It is very kind of you, martinhjelmare. Now my distance sensor shows the continuously updated value in the Front End. and the log file still reports that File does not exist or is not readable. Anyway it works. Thank you very much for your help,

Have you restarted home assistant and are you sure the log is from the latest restart?

Hello martinhjelmare, After I restarted Raspberry Pi, then I checked the log file again. I found that the message “File does not exist or is not readable” already disappeared. I can’t find it anymore, Many thanks.

1 Like

Hello @kiratikarnpat Glad you got it working! I’m running HASS in ubuntu using the virtualenv. I wonder if that’s why I cannot create a persistence file? I have it set as per below, but still get persistence file errors…

mysensors:
  gateways:
    - device: mqtt
      persistence_file: '/home/hass/.homeassistant/mysensors.json'
      topic_in_prefix: 'P'
      topic_out_prefix: 'S'
  debug: true
  optimistic: false
  persistence: true
  retain: true
  version: 2.0

Hello, shep. At first, I let Home Assistant create persistence file for me automatically by deleting the persistence_file line out of the mysensors configuration. Because I didn’t know where the correct path to put the file on. Then Home Assistant create a file format .pickle in the root directory of home assistant. I tried to open the file, it was open with uninterpretable characters which I didn’t know why. So I just extracted the obtained path and add persistence_file line to that path persistence_file: ‘/var/opt/homeassistant/mysensors1.json’ and then Home Assistant created a readable mysensors1.json for me. I also had wire connections trouble which caused unsuccessful connection from sensor to Serial Gateway many times. The wire connections was not secured enough. When I move the sensor to new place, It always has connection problem almost every times. I think that loose wire connections was my main cause of my first trial failure. I am thinking about shouldering some of the loose wire connection. I wish It will work for you too.

Well I have tried letting HASS create the persistence file, by removing the reference in configuration.yaml, but still get the following errors in the log:

16-10-26 17:34:24 mysensors.mysensors: File does not exist or is not readable: /home/hass/.homeassistant/mysensors1.pickle
16-10-26 17:34:24 mysensors.mysensors: Trying backup file: /home/hass/.homeassistant/mysensors1.pickle.bak
16-10-26 17:34:24 mysensors.mysensors: File does not exist or is not readable: /home/hass/.homeassistant/mysensors1.pickle.bak
16-10-26 17:34:24 mysensors.mysensors: Failed to load sensors from file: /home/hass/.homeassistant/mysensors1.pickle

Below is the output of my configuration.yaml:

###  MQTT  ###
mqtt:
  broker: 192.168.0.90
  port: 1883
  client_id: home-assistant
  keepalive: 60
  username: redacted
  password: redacted
###  MySensors  ###
mysensors:
  gateways:
    - device: mqtt
      topic_in_prefix: 'P'
      topic_out_prefix: 'S'
  debug: true
  optimistic: false
  persistence: true
  retain: true
  version: 2.0

I cannot understand what I could be doing wrong? I have restarted HASS a few times also, just to see as I read a few posts that it makes a difference, but no luck…

I can see the messages being displayed on the arduino gateway, and within the Mosquitto server running on the same host as HASS.

Any ideas?

Thanks all

Hello shep, Have you looked into read/write folder permission? Yesterday I removed my distance sensor and replaced with gas sensor. But it was unsuccessful. Then I have to restart the distance sensor all over again. It still hasn’t appeared in Home Assistant. But it created .json file with initial data only, no distance sensor data yet. So I have to go back to Arduino Serial monitor again to see whether transmission is ok. I also have to work on.