MQTT sensor reporting several different sensors on same topic, cluttering history

Hi!

I’ve got another gateway (futurehome) running z-wave and zigbee components, and I’m just started to read out some of them via MQTT.

My problem is that one of the units (Heat-it z-relay) is reporting kWh, W and A on the same topic, and that clutters my history on the sensor in HA.

This is my setup for sensor:

  # VVB
  - platform: mqtt
    name: "Energiforbruk VVB"
    state_topic: "pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1"
    unit_of_measurement: 'kWh'
    value_template: "{{ value_json.val }}"
    unique_id: "pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1"

This is the history for the sensor, as you can see, it’s totally wrong, those values should only be increasing during the day:
Skjermbilde 2021-11-19 171756

Here is the payloads from futurehome, all 3 coming on topic: pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1:

{
  "ctime" : "2021-11-19T17:06:39+0100",
  "props" : {
    "unit" : "kWh"
  },
  "serv" : "meter_elec",
  "tags" : [ ],
  "type" : "evt.meter.report",
  "val" : 211.130004882812,
  "val_t" : "float"
}
{
  "ctime" : "2021-11-19T17:06:39+0100",
  "props" : {
    "unit" : "W"
  },
  "serv" : "meter_elec",
  "tags" : [ ],
  "type" : "evt.meter.report",
  "val" : 315.0,
  "val_t" : "float"
}
{
  "ctime" : "2021-11-19T17:06:39+0100",
  "props" : {
    "unit" : "A"
  },
  "serv" : "meter_elec",
  "tags" : [ ],
  "type" : "evt.meter.report",
  "val" : 1.39999997615814,
  "val_t" : "float"
}

I’m not super familiar with this so somebody may come along with better info, but thought I’d share my $0.02 just in case.

Your best bet would probably be to configure your Heat-it z-relay to publish the various information to different topics using variables.

But, to achieve this only on the HA side, you’ll probably have to configure one sensor to read the different values provided in the JSON into different attributes using the configuration variables described here. Then, only update this sensor if the unit of measurement is kWh.

1 Like

Assuming you only care for kWh, this should work

# VVB
  - platform: mqtt
    name: "Energiforbruk VVB"
    state_topic: "pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1"
    unit_of_measurement: 'kWh'
    value_template: "{{ value_json.val if value_json.props.unit=='kWh' else states('sensor.energiforbruk_vvb') }}"
    unique_id: "pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1"

EDIT: Fixed quotes

2 Likes

Thank you so much for your suggestions.

However, if I try the code mentioned above in Developer → Template, it gives me this error:
UndefinedError: 'value_json' is undefined

If I try it in configuration.yaml, it gives me this error:

can not read an implicit mapping pair; a colon is missed at line 113, column 115:
     ... ('sensor.energiforbruk_vvb') }}"
                                         ^

I can’t find the fault, it is likely there, but…

If you use double-quotes outside the template, use single-quotes inside the template. In the posted example, there are double-quotes outside and inside the template. Replace the inner double-quotes ("kWh") with single-quotes ('kWh').

value_template: "{{ value_json.val if value_json.props.unit=='kWh' else states('sensor.energiforbruk_vvb') }}"
2 Likes

Thanks to you, I finally got it working. This is the working result:

  - platform: mqtt
    name: "Energiforbruk VVB"
    device_class: energy
    state_class: total_increasing
    expire_after: 3600
    state_topic: "pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1"
    unit_of_measurement: 'kWh'
    value_template: "{{ value_json.val if value_json.props.unit=='kWh' else states('sensor.energiforbruk_vvb') }}"

But, a new issue has emerged, the sensor reports the kWh, but almost all the time HA is reporting “unavailable” on the sensor.
Can this problem be related to all the other values reporting on same topic but are filtered out?
I’ve tried same setup, but on a device just reporting temperature on the topic, that one never has the “unavailable” issue.
Skjermbilde 2021-12-11 152021
Skjermbilde 2021-12-11 151943

Never mind, sorted it out, seemed like i had duplicate sensors, deleted one of them and everything works like a charm.
I’ve added a unique id and rounded the sensor result, like below, but I don’t think that was relevant to the last issue.

  - platform: mqtt
    name: "Energiforbruk VVB"
    device_class: energy
    state_class: total_increasing
    expire_after: 3600
    state_topic: "pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1"
    unit_of_measurement: 'kWh'
    value_template: "{{ value_json.val if value_json.props.unit=='kWh' else states('sensor.energiforbruk_vvb')|round(2) }}"
    unique_id: "pt:j1/mt:evt/rt:dev/rn:zw/ad:1/sv:meter_elec/ad:38_1"

Thank you so much!