Mqtt persistence file and mysensors

Hi everyone:

I’m trying to implement MySensors 2 with MQTT and I keep getting a file permission error:

16-10-01 10:09:34 mysensors.mysensors: File does not exist or is not readable: /tmp/mysensors.json
16-10-01 10:09:34 mysensors.mysensors: Trying backup file: /tmp/mysensors.json.bak
16-10-01 10:09:34 mysensors.mysensors: File does not exist or is not readable: /tmp/mysensors.json.bak
16-10-01 10:09:34 mysensors.mysensors: Failed to load sensors from file: /tmp/mysensors.json

I’m running HAS on ubuntu in a virtualenv and it appears that the persistence_file cannot be created no matter where I specify it:

I’m just wondering if perhaps I’m doing something incorrectly?

mqtt:
  broker: 192.168.1.1
  port: 1883
  client_id: home-assistant
  keepalive: 60
  username: USERNAME
  password: PASSWORD
  protocol: 3.1
###  MySensors  ###
mysensors:
  gateways:
    - device: '192.168.1.2'
      persistence_file: '/tmp/mysensors.json'
      tcp_port: 5003
  debug: true
  optimistic: false
  persistence: true
  retain: true
  version: 2.0

Thanks!

I should also specify as a test I am using the following MQTT include file for two sensors to test with:

- platform: mqtt
  name: "Basement Rec Room Temperature"
  state_topic: "P/0/1/1/0/0"
  unit_of_measurement: "°C"
- platform: mqtt
  name: "Basement Rec Room Humidity"
  state_topic: "P/0/0/1/0/1"
  unit_of_measurement: "%"

The first time you start home assistant, you will inevitably get those warnings cause the persistence file won’t have been created yet. It will be created when sensors are updated.

Your configuration is for an ethernet gateway but you said you want to use mqtt.

Hi @martinhjelmare thanks for your reply, and appologies earlier as I was playing with a few config’s. My correct config for my configuration.yaml file is:

###  MQTT  ###
mqtt:
  broker: 192.168.0.1
  port: 1883
  client_id: home-assistant
  keepalive: 60
  username: username
  password: password
  protocol: 3.1
###  MySensors  ###
mysensors:
  gateways:
    - device: mqtt
      persistence_file: '/tmp/mysensors.json'
      topic_in_prefix: 'P'
      topic_out_prefix: 'S'
  debug: true
  optimistic: false
  persistence: true
  retain: true
  version: 2.0

I have mosquitto running on the same box as HAS. I have an MQTT gateway at a separate address, that works great when connecting to it to either publish a relay change, watch a PIR being triggered from a remote sensor, etc. but for some odd reason the persistence file never get’s created ( I currently have it going to /tmp but never seems to be created, and I keep getting these errors in the log

16-10-02 20:48:17 mysensors.mysensors: File does not exist or is not readable: /tmp/mysensors.json
16-10-02 20:48:17 mysensors.mysensors: Trying backup file: /tmp/mysensors.json.bak
16-10-02 20:48:17 mysensors.mysensors: File does not exist or is not readable: /tmp/mysensors.json.bak
16-10-02 20:48:17 mysensors.mysensors: Failed to load sensors from file: /tmp/mysensors.json

I am assuming without a persistence file, HAS has an issue with the sensors / nodes. Is this correct?

FYI Also below is my gateway ino script, mostly from @Mihai ( thanks very much Mihai for a lot of great tips / configurations issues! )


// This MQTT gateway works on Arduino UNO

// Enable debug prints to serial monitor - does not fit the UNO board with actual defined sensors!
#define MY_DEBUG 

// Enables and select radio type (if attached)
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#define MY_GATEWAY_MQTT_CLIENT

// Set this nodes subscripe and publish topic prefix
#define MY_MQTT_PUBLISH_TOPIC_PREFIX "P"
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "S"

// Set MQTT client id
#define MY_MQTT_CLIENT_ID "0"

#define MY_GATEWAY_W5100
#define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED

// W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
//#define MY_W5100_SPI_EN 4  

// Enable Soft SPI for NRF radio (note different radio wiring is required)
// The W5100 ethernet module seems to have a hard time co-operate with 
// radio on the same spi bus.
// The PINs mentioned here are for Arduino Mega2560 ! Color code is from MySensor site
#if !defined(MY_W5100_SPI_EN)
  #define MY_SOFTSPI
  #define MY_SOFT_SPI_SCK_PIN 14 // A0, Green
  #define MY_SOFT_SPI_MISO_PIN 16  // A2, Violet
  #define MY_SOFT_SPI_MOSI_PIN 15  // A1, Blue
#endif  

// When W5100 is connected we have to move CE/CSN pins for NRF radio
#define MY_RF24_CE_PIN 5  // Orange
#define MY_RF24_CS_PIN 6  // Yellow

// Enable these if your MQTT broker requires usenrame/password
#define MY_MQTT_USER "username"
#define MY_MQTT_PASSWORD "password"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
#define MY_IP_ADDRESS 192,168,0,X

// If using static ip you need to define Gateway and Subnet address as well
#define MY_IP_GATEWAY_ADDRESS 192,168,0,X
#define MY_IP_SUBNET_ADDRESS 255,255,255,0


// MQTT broker ip address.  
#define MY_CONTROLLER_IP_ADDRESS 192,168,0,X

// The MQTT broker port to to open 
#define MY_PORT 1883      

/*
// Flash leds on rx/tx/err
#define MY_LEDS_BLINKING_FEATURE
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300
// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE
// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60 
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN  3 
#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
*/

#include <Ethernet.h>
#include <SPI.h>
#include <MySensor.h>
#include <DHT.h> 

#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1

// Set this offset if the sensor has a permanent small offset to the real temperatures
#define SENSOR_TEMP_OFFSET -1
/*
Ethernet use the following PINs, do not use them:
UNO:  4, 10, 11, 12, 13
*/
#define HUMIDITY_SENSOR_DIGITAL_PIN 3
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)

DHT dht;
float lastTemp;
float lastHum;
boolean metric = true; 
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);

void setup() 

{ 
  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 

  metric = getConfig().isMetric;
}

void presentation()
{ 
  // Send the sketch version information to the gateway
  sendSketchInfo("TemperatureAndHumidityGateway", "1.0");

  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID_HUM, S_HUM);
  present(CHILD_ID_TEMP, S_TEMP);
}


void loop()
{  
  delay(dht.getMinimumSamplingPeriod());
 
  // Fetch temperatures from DHT sensor
  float temperature = dht.getTemperature();
  if (isnan(temperature)) {
      Serial.println("Failed reading temperature from DHT");
  } else if (temperature != lastTemp) {
    lastTemp = temperature;
    if (!metric) {
      temperature = dht.toFahrenheit(temperature);
    }
    send(msgTemp.set(temperature, 1));
    #ifdef MY_DEBUG
    Serial.print("T: ");
    Serial.println(temperature);
    #endif
  }
  
  // Fetch humidity from DHT sensor
  float humidity = dht.getHumidity();
  if (isnan(humidity)) {
      Serial.println("Failed reading humidity from DHT");
  } else if (humidity != lastHum) {
      lastHum = humidity;
      send(msgHum.set(humidity, 1));
      #ifdef MY_DEBUG
      Serial.print("H: ");
      Serial.println(humidity);
      #endif
  }
  
  sleep(SLEEP_TIME); //sleep a bit
}

Please edit your post and put the sketch within code block markdown.

You can’t put the gateway to sleep. That’s one problem I see.

I’d like to see a debug log from HA also.

@shep

Did you resolve this?
I’m seeing the exact same thing.

@martinhjelmare
New sensors also don’t get a node id it seems.
When I have time I will grep a log from the node and hass.

@martinhjelmare Sorry for the bad post, I thought the code was blocked properly but I guess not. I still seem to be getting the following errors in log:

16-10-16 12:40:55 mysensors.mysensors: File does not exist or is not readable: /tmp/mysensors.json
16-10-16 12:40:56 mysensors.mysensors: Trying backup file: /tmp/mysensors.json.bak
16-10-16 12:40:56 mysensors.mysensors: File does not exist or is not readable: /tmp/mysensors.json.bak
16-10-16 12:40:56 mysensors.mysensors: Failed to load sensors from file: /tmp/mysensors.json

@martinhjelmare might you have any idea? Thanks again

You don’t need to care about those warnings the first time you start home assistant without sensors already presented and shown in the gui. Once the sensors are added to HA, the persistence file will be created.

Unfortunately persistence is broken currently for the MQTT gateway in home assistant. I hope to have this fixed in the next version.

Anyway, there are several issues in your sketch. Most ip addresses are not defined properly. You can’t end them with an X. Use the proper four numbers of the ip addresses that you want to use separated with commas.

You should not use delay in your sketch but wait. Wait will process messages in the background.

You can’t use sleep for a gateway. The gateway must be able to process messages all the time.

When you have fixed those issues and can ping the gateway, please activate debug level logging using the logger component and post all mysensors related lines after start.

Thanks @martinhjelmare very much for the reply. Please note I just removed the last octet of the IP for the post. Let me try the gateway with WAIT vs sleep. I have a temp sensor also hooked up the gateway, and the only way I could get it to calm down was using sleep, but I will retry. That said, the gateway itself DOES indeed see my remote sensors. I am just unable to get them to appear in HASS. I have statically assigned client ID’s as you can see above, but for some reason the persistence file never seems to get created, similiar to this post as well: