Skip to content

Instantly share code, notes, and snippets.

@ndfred
Last active August 17, 2023 11:31
Show Gist options
  • Save ndfred/5bb51b567f8cfaf2bb06da6393169321 to your computer and use it in GitHub Desktop.
Save ndfred/5bb51b567f8cfaf2bb06da6393169321 to your computer and use it in GitHub Desktop.

⚠️ There is now an official way to connect Glow to Home Assistant via MQTT, I would suggest using that instead of the method described below. Here are the sensor templates for that (replace GLOW_DEVICE_ID with the relevant information for your device):

mqtt:
  sensor:
    - name: "Home Power"
      state_topic: "glow/GLOW_DEVICE_ID/SENSOR/electricitymeter"
      unit_of_measurement: 'W'
      value_template: "{{ (value_json['electricitymeter']['power']['value'] * 1000) | int }}"
      icon: 'mdi:flash'

    - name: "Home Energy"
      state_topic: "glow/GLOW_DEVICE_ID/SENSOR/electricitymeter"
      unit_of_measurement: 'kWh'
      state_class: 'total_increasing'
      device_class: 'energy'
      unique_id: 'home_energy'
      value_template: "{{ value_json['electricitymeter']['energy']['import']['cumulative'] }}"

    - name: "Home Daily Energy"
      state_topic: "glow/GLOW_DEVICE_ID/SENSOR/electricitymeter"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['electricitymeter']['energy']['import']['day'] }}"
      icon: 'mdi:flash'

    - name: "Home Gas"
      state_topic: "glow/GLOW_DEVICE_ID/SENSOR/gasmeter"
      unit_of_measurement: 'm³'
      state_class: 'total_increasing'
      device_class: 'gas'
      unique_id: 'home_gas'
      value_template: "{{ value_json['gasmeter']['energy']['import']['cumulativevol'] }}"

    - name: "Home Daily Gas"
      state_topic: "glow/GLOW_DEVICE_ID/SENSOR/gasmeter"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['gasmeter']['energy']['import']['day'] }}"
      icon: 'mdi:flash'

Here is a way to expose most of the Glow Display MQTT information to home assistant through templates. Please make sure you ask Hildebrand support to enable your account for MQTT access before setting everything up.

The first step is to connect Home Assistant to MQTT. If you don't have your own broker hooked up yet, go ahead and set it up (I would suggest using the Integrations UI) and specify your Glow username and password as well as glowmqtt.energyhive.com as the server and 8883 as the port to make sure we're establishing a secure SSL connection.

If you already have your own MQTT server hooked up to Home Assistant, like I do, you will have to set up a bridge between your MQTT server and the Glow one, after that your server will mirror the MQTT messages from the Glow server. You have to do this because Home Assistant does not support connecting to more than one MQTT server.

I will assume you are using Mosquitto as an MQTT server. Start by creating a file named /etc/mosquitto/conf.d/glow.conf with the following content (replace GLOW_USERNAME, GLOW_PASSWORD and GLOW_DEVICE_ID with your relevant information):

connection glowmqtt
address glowmqtt.energyhive.com:8883
remote_username GLOW_USERNAME
remote_password GLOW_PASSWORD
bridge_capath /etc/ssl/certs/
topic SMART/HILD/GLOW_DEVICE_ID in 0

Once that is done, just restart Mosquitto to reload its configuration:

sudo systemctl restart mosquitto

Now you should be able to see your Glow messages over MQTT by using your client of choice.

If that works all right, you can open the Home Assistant configuration.yaml file (for me it is located at /home/homeassistant/.homeassistant/configuration.yaml) and add these new sensors under the sensor section (replace GLOW_DEVICE_ID with the relevant information for your device):

mqtt:
  sensor:
    - name: "Home Power"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'W'
      value_template: "{{ value_json['elecMtr']['0702']['04']['00']|int(base=16) }}"
      icon: 'mdi:flash'

    - name: "Home Energy"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'kWh'
      state_class: 'total_increasing'
      device_class: 'energy'
      unique_id: 'home_energy'
      value_template: "{{ value_json['elecMtr']['0702']['00']['00']|int(base=16) * value_json['elecMtr']['0702']['03']['01']|int(base=16) / value_json['elecMtr']['0702']['03']['02']|int(base=16) }}"

    - name: "Home Daily Energy"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['elecMtr']['0702']['04']['01']|int(base=16) * value_json['elecMtr']['0702']['03']['01']|int(base=16) / value_json['elecMtr']['0702']['03']['02']|int(base=16) }}"
      icon: 'mdi:flash'

    - name: "Home Weekly Energy"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['elecMtr']['0702']['04']['30']|int(base=16) * value_json['elecMtr']['0702']['03']['01']|int(base=16) / value_json['elecMtr']['0702']['03']['02']|int(base=16) }}"
      icon: 'mdi:flash'

    - name: "Home Monthly Energy"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['elecMtr']['0702']['04']['40']|int(base=16) * value_json['elecMtr']['0702']['03']['01']|int(base=16) / value_json['elecMtr']['0702']['03']['02']|int(base=16) }}"
      icon: 'mdi:flash'

    - name: "Home Gas"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'm³'
      state_class: 'total_increasing'
      device_class: 'gas'
      unique_id: 'home_gas'
      value_template: "{{ value_json['gasMtr']['0702']['00']['00']|int(base=16) * value_json['gasMtr']['0702']['03']['01']|int(base=16) / value_json['gasMtr']['0702']['03']['02']|int(base=16) }}"

    - name: "Home Daily Gas"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['gasMtr']['0702']['0C']['01']|int(base=16)  * value_json['gasMtr']['0702']['03']['01']|int(base=16) / value_json['gasMtr']['0702']['03']['02']|int(base=16) }}"
      icon: 'mdi:flash'

    - name: "Home Weekly Gas"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['gasMtr']['0702']['0C']['30']|int(base=16)  * value_json['gasMtr']['0702']['03']['01']|int(base=16) / value_json['gasMtr']['0702']['03']['02']|int(base=16) }}"
      icon: 'mdi:flash'

    - name: "Home Monthly Gas"
      state_topic: "SMART/HILD/GLOW_DEVICE_ID"
      unit_of_measurement: 'kWh'
      value_template: "{{ value_json['gasMtr']['0702']['0C']['40']|int(base=16)  * value_json['gasMtr']['0702']['03']['01']|int(base=16) / value_json['gasMtr']['0702']['03']['02']|int(base=16) }}"
      icon: 'mdi:flash'

If you would like to set up the energy dashboard, pick Home Energy for electricity and Home Gas for gas.

Now restart your Home Assistant instance, and you should be good to go! All the sensors should appear in the developer tools menu, for instance sensor.home_instant_energy, and update in real time.

If you'd like to play with the MQTT API, you can also check out my python code to connect and fetch data directly from the MQTT server.

@norfolkmustard
Copy link

Yeah, off again. Was working overnight, lost it around 0540
4B12539F-F23E-4D8E-B9DB-42A9F79E4FBB

@BenHarris
Copy link

The "Home Gas" sensor seems to be returning kWh and not M3. Any ideas?

@townsmcp
Copy link

townsmcp commented Apr 6, 2022

@BenHarris it will depend on what unit of measurement your gas meter is sending. Check line 21 of the following and compare to your MQTT values:
https://gist.github.com/ndfred/b373eeafc4f5b0870c1b8857041289a9
I have the opposite issue; meter reports in M3 but whenever I convert m3 to kWh everything goes screwy

@BenHarris
Copy link

Right, Table D-25 on page 236 of the smart energy profile spec (https://zigbeealliance.org/wp-content/uploads/2019/12/docs-07-5356-19-0zse-zigbee-smart-energy-profile-specification.pdf) says 0207 0x0300 shows the unit of measure. Mine is "00", which when referenced to Table D-26 shows 0x00 = kWh

@townsmcp
Copy link

townsmcp commented Apr 6, 2022

@BenHarris so is the problem you want to store M3 or that the sensor is setup to capture in M3 but you want to correct it to capture in kWh?
If the latter, amend the sensor code to the following:

unit_of_measurement: 'kWh'
device_class: energy
state_class: total_increasing

@BenHarris
Copy link

I really just want to get the data into the energy dashboard. I deleted the m3 sensor, changed it to kWh as you said. This allowed me to add it to the gas part of the energy dashboard, although it has now been 24 hours, and no data has appeared. Wondering if the rogue m3 data is still in there somewhere causing problems

@norfolkmustard
Copy link

Check in Developer Tools > Statistics to see if there are issues it knows about

@townsmcp
Copy link

townsmcp commented Apr 8, 2022

@BenHarris have you checked your sensor is using the correct register value for historic readings? For example the sensors listed above use [0C] however my own sensors need to use [04]. [0C] is the AlternativeHistoric
Eg above shows "{{ value_json['gasMtr']['0702']['0C']['01']|int(base=16) but my sensor has to be "{{ value_json['gasMtr']['0702']['04']['01']|int(base=16)

Also, another gotcha that I seem to be encountering is after a reboot my gas and electric sensor values go nuts. I’m trying to find out how to stop the reboot taking a zero value and then adding the meter value to old value eg gas was 0.4m3 before reboot. After reboot and allow the hour to pass it went to 24,000 m3.

@BenHarris
Copy link

@townsmcp I've double-checked the register values and they are definitely correct. I can also see the correct value in Developer Tools.

There are no issues listed under the statistics section for the gas sensor. I checked the db, and the old m3 statistics were still in there though (even though I had deleted the sensor), so I manually deleted those and restarted HA. I'll give it a couple of hours and see if that sorts it!

@BenHarris
Copy link

Also, another gotcha that I seem to be encountering is after a reboot my gas and electric sensor values go nuts. I’m trying to find out how to stop the reboot taking a zero value and then adding the meter value to old value eg gas was 0.4m3 before reboot. After reboot and allow the hour to pass it went to 24,000 m3.

I suspect this issue relates to using total_increasing, and a value not being available on the mqtt server yet. The announcement about the total_increasing state class says "a decreasing value is interpreted as the start of a new meter cycle or the replacement of the meter". I think using total instead might be a better option and prevent this from happening.

@townsmcp
Copy link

townsmcp commented Apr 8, 2022

@BenHarris thanks for the advice. After deleting the M3 sensor and rebooting/ restarting manually configured mqtt sensors, did you check the statistics in dev, see the rogue long term stats, choose to delete the data, then add you new sensor with old sensor name? Maybe try a completely different sensor name?

Unfortunately I’m already using total in the sensor and still get the issue:

sensor:
  - platform: mqtt
    name: "Gas Meter M3"
    state_topic: !secret smart_hild_state_topic
    last_reset_value_template: homeassistant.util.dt.utc_from_timestamp(0)
    unit_of_measurement: m³
    value_template: "{{ value_json['gasMtr']['0702']['00']['00']|int(base=16) * value_json['gasMtr']['0702']['03']['01']|int(base=16) / value_json['gasMtr']['0702']['03']['02']|int(base=16) }}"
    icon: 'mdi:counter'
    device_class: gas
    state_class: total

@townsmcp
Copy link

Does any know how to drop specific value readings that come through in mqtt? I have been talking with Hildebrand about the spikes I keep getting, specifically in gas meter readings, and they have come back with:

`We confirm that the meter has returned a value of "FFFFFFFFFFFF", which for the data type means it's an invalid value.

The ingress to the Hildebrand database detects these invalid values and discards them so they don't appear in our APIs (and therefore Bright); we assume they do appear in the MQTT feed and so will need to be detected and discarded by MQTT users.`

it certainly explains why I’m getting gas readings at various times even though gas should only come through every half hour

@pg-nut
Copy link

pg-nut commented Apr 23, 2022

I bring in the raw reading and then use a template as below to remove the zeros:

gas_meter_reading:
  friendly_name: "Gas Meter Reading"
  unit_of_measurement: 'kWh'
  value_template: >-
      {% if states("sensor.gas_meter_raw") | float(0) > 0 %}
      {{ states("sensor.gas_meter_raw") }}
      {%- else -%}
      {{ states("sensor.gas_meter_reading") }}
      {% endif %}

@townsmcp
Copy link

townsmcp commented Apr 23, 2022

@pg-nut thanks. Being as "FFFFFFFFFFFF" in hex converts to 281474976710655 in decimal and Hildebrand have said they exclude "FFFFFFFFFFFF" from any readings, would the following then work (so that I am excluding that as well as 0)? Or would the register value be a signed 2's complement and therefore convert as n/a? If n/a, would that be a 0 value registered in HA?

gas_meter_reading:
  friendly_name: "Gas Meter Reading"
  unit_of_measurement: 'kWh'
  value_template: >-
      {% if states("sensor.gas_meter_raw") | float(0) > 0 %}
      {{ states("sensor.gas_meter_raw") }}
      {% elif states("sensor.gas_meter_raw") | float(0) == 281474976710655 %}
      {{ states("sensor.gas_meter_reading") }}
      {%- else -%}
      {{ states("sensor.gas_meter_reading") }}
      {% endif %}

@pg-nut
Copy link

pg-nut commented Apr 24, 2022

When I was having problems, I looked at my “raw” sensor history and could see that every night it spiked down to zero and that was why I setup my template that way. Maybe you could see what your rogue value is in the same way and adjust the template accordingly?

@ndfred
Copy link
Author

ndfred commented Aug 15, 2022

It’s been a while since I have checked in here, nice to see people are still fixing issues!

I’ll refresh my config soon and expect to make the following changes, let me know if there is anything else you’d like to see or stuff I should know about:

  • share my energy dashboard configuration which took WAY too long to set up properly
  • leverage the new local MQTT connection to hopefully improve privacy / latency / resilience
  • filter out spikes and invalid values in the gas measurements in particular

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment