MQTT sensor setup

Hi,

I’ve got Resol vBus up and running (which basically reads data from my solar heating system).
It results in the following json data that could be sent via MQTT to Home Assistant. See data below …
I know that I can create an MQTT sensor according to https://home-assistant.io/components/sensor.mqtt/ but I have no idea on how to do it.

TIA,
Tom

[
    {
        "sensor": "Temperature sensor 1",
        "value": -10.600000000000001
    },
    {
        "sensor": "Temperature sensor 2",
        "value": 41.5
    },
    {
        "sensor": "Temperature sensor 3",
        "value": 23
    },
    {
        "sensor": "Temperature sensor 4",
        "value": 19.6
    },
    {
        "sensor": "Temperature sensor 5",
        "value": 37
    },
    {
        "sensor": "Temperature sensor 6",
        "value": -10.9
    },
    {
        "sensor": "Relay status 1",
        "value": 0
    },
    {
        "sensor": "Relay status 2",
        "value": 0
    },
    {
        "sensor": "Relay status 3",
        "value": 0
    },
    {
        "sensor": "Relay status 4",
        "value": 0
    },
    {
        "sensor": "Relay status 5",
        "value": 0
    },
    {
        "sensor": "Relay status 6",
        "value": 0
    },
    {
        "sensor": "Pump speed 1",
        "value": 0
    },
    {
        "sensor": "Pump speed 2",
        "value": 0
    },
    {
        "sensor": "Pump speed 3",
        "value": 0
    },
    {
        "sensor": "Error number",
        "value": 0
    },
    {
        "sensor": "Error mask",
        "value": 0
    },
    {
        "sensor": "Error info 1",
        "value": 0
    },
    {
        "sensor": "Error info 2",
        "value": 0
    },
    {
        "sensor": "Error info 3",
        "value": 0
    },
    {
        "sensor": "Error info 4",
        "value": 0
    },
    {
        "sensor": "Arrangement options 1",
        "value": 0
    },
    {
        "sensor": "System",
        "value": 11
    },
    {
        "sensor": "Version",
        "value": 258
    },
    {
        "sensor": "Module status",
        "value": 0
    },
    {
        "sensor": "System time",
        "value": 1370
    }
]

Just setting up the sensor like so

- platform: mqtt
  state_topic: 'home-assistant/bla'
  force_update: true

results in:

2018-02-24 23:10:00 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/entity.py", line 266, in async_update_ha_state
    self.entity_id, state, attr, self.force_update)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/core.py", line 745, in async_set
    state = State(entity_id, new_state, attributes, last_changed)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/core.py", line 540, in __init__
    "State max length is 255 characters.").format(entity_id))
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.test. State max length is 255 characters.

I understand that state is not allowed to be >255 chars, but I don’t want to set a state at all (if possible), just attributes for that sensor.
It would be great if that MQTT sensor would display attributes from the json, basically like ‘sun’ does:

I am using mosquito but it should be similar. I the configuration file I have:

mqtt:
  broker: core-mosquitto
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery: true

In the configuration file under sensors:

  - platform: mqtt
    state_topic: 'office/sensor1'
    name: 'Temperature'
    unit_of_measurement: '°F'
    value_template: '{{ value_json.temperature }}'
  - platform: mqtt
    state_topic: 'office/sensor1'
    name: 'Humidity'
    unit_of_measurement: '%'
    value_template: '{{ value_json.humidity }}'
  - platform: mqtt
    state_topic: 'office/sensor1'
    name: 'Barometric Pressure'
    unit_of_measurement: 'mb'
    value_template: '{{ value_json.pressure }}'

Are you using HA or HASSIO?

Hi @tmeringer !

The issue of >255 characters is something I’ve only come to grips with myself.

The REST sensor will map a JSON payload directly to attributes, but the 255 character limit still applies, so is not a solution to your problem.

The trick is to separate out just the information you need for a particular sensor, and create multiple sensors. Once for each piece of data you wish to display.

Your JSON is in the form of an array as indicated by the opening and closing square brackets [ ]
This makes accessing your data quite easy as you can refer directly to individual data sets using a value_template and specifying the array index.

Lets say for example that you wanted a sensor that showed the value of temperature sensor 3 from your JSON.

From your JSON payload, you have 26 sets of data, starting with Temp Sensor 1 and ending with System Time. The array starts counting at 0. So the Temp Sensor 3 data is sitting in the array at index 2 ( 0,1,2 ), remembering that Temp Sensor 1 is array index 0.

With this info above, you can now add the following line to your sensor mentioned in your OP

value_template: {{ value_json[2].value }}

The ‘value_json’ is telling HA thats its going to be working with JSON
The ‘[2]’ is the index number of the data that you want to access
The ‘value’ after the full stop is the string you want to retrieve the value of in the data set at index [2]

In your JSON, there are two strings in each data set, “sensor” and “value”.
So if your wanted to return the name of the sensor instead of the numerical value, you would replace .value with .sensor

If you want more values, just create another sensor referring the corresponding index value of the data you want.

1 Like

What topic are you publishing your MQTT message with?

home-assistant/bla

Whow. What a detailed explanation. Thanks for that.
Too bad that it cannot be done using a single sensor that provides multiple attributes (like the ‘sun’ example I provided). I’ll test using multiple sensors soon …

HA on Debian.

You can get attributes from a json payload - see this thread.

Unfortunately, your json is an array rather than named parameters, so I think that won’t work. I think your best option is a different sensor for each element of the array. Is there some reason why that isn’t acceptable? It is normally easier to use for automations.

No. I created multiple sensors now and it works perfectly fine.
Thanks to all that helped here! :+1:

Tom,
I have read your bulletins with great interest as I too have a Resol controller on my Solar Thermal system. The Resol data logger is ridiculously expensive and over re past two years I have read several articles on the web that have developed “home brew” interfaces etc but these have been a bit too complicated for me to follow and I haven’t had the time to progress them.
I got around this using an alternative system and some temperature sensors strapped to the pipework but this is not really effective. The resol system has pump data which is key to calculating energy savings.
I have now discovered HA and have returned to looking at the Resol controller challenge and am hoping you would be kind enough to answer a couple of questions. Please forgive the simplicity and possibly the technical incorrectness – I’m really out on the boundaries of my knowledge of this stuff here!
Many thanks in advance.

Q1. Is my understanding of the connection concept correct…?

  1. Resol Device is connected to a …
  2. A collector device.
  3. Data from the Resol device is transmitted via VBus to the the collector device.
  4. JSON code on the collector receives the data and parses it and presents to a MQTT server.
  5. The HA device/App pulls the data from MQTT server

Q2. What do you use as a “collector” to receive the data from the Resol device(s)? Is it a raspberry pi? If so is it just a raspberry pi or is there something else plugged into it like an Arduino board?

Q3. How do you connect the collector to the Resol device?
a) Did you build some sort of circuit board like this?, or
b) a USB 2.0 to RS485 Serial Converter Adapter like this? Or
c) another method?
Do you know if option (b) would be viable and if © can you provide details or pointers to what this is.

Q3. The JSON code runs on the collector?

Q4. You say that you have created multiple sensors (I assume as per jivesinger’s recommendation). I don’t follow what is being explained in the recommendation. Are the multiple sensors created within the JSON code or as part of the HA configuration? Could you provide an example of your config so that I could better understand it?

Q5. I’m using HASS.io Home Assistant 0.65.6. on a dedicated Raspberry Pi 3. Would this be compatible with your approach?

I’ve got a Wagner SunGo XL control device in the basement. That’s wired to the solar panel on my house’s roof, to whatever relays and pumps.
I’ve modified resol-vbus/examples/json-live-data-server at master · danielwippermann/resol-vbus · GitHub so that it sends MQTT messages to Mosquitto running on the Raspi where Home Assistant runs.

It’s a Raspi 3 with a USB-Serial-converter. The Wagner SunGo XL came with a small serial interface that can be used to read data. That’s plugged into the USB/Serial converter on the Raspi.

See previous reply.

I don’t know.

It runs on the Raspi.

They are created in Home Assistant.

Yes.

This post explains a lot, however (since I am a total noob) maybe you could give me some help with this? I have managed to push data data from a sensor to mqtt with the following output:

Client mosqsub|28683-hassbian received PUBLISH (d0, q0, r0, m0, '/Airthings/wave1', ... (93 bytes))
{"wave": {"Temperature": 19.0,"Humidity": 25.0,"Radon24havg": 100.0,"Radonlongterm": 100.0 }}

Will it then be correct to add the last sensor data into HA like this? Or will it be different due to the first “wave”: … statement?

  - platform: "mqtt"
    name: "Radon Longtherm"
    state_topic: "/Airthings/wave1"
    unit_of_measurement: "Bq/m3"
    icon: "mdi:radioactive"
    value_template: "{{ value_json[3].value }}"
    force_update: true
   value_template: "{{ value_json.wave.Radonlongterm }}"

Should get the ‘last’ value. In this payload, everything is addressed by a name ( the {} in the payload indicates this), so you need to change the name in the value_template to get the appropriate sensor.

@gpbenton, aha that makes sense, thanks!