MQTT negative value issues

I have a Bright CAD device (SMETS Secure 1) and have managed to integrate this into my HASS with MQTT (At long last).

Whenever I get a minus figure for my use (i.e. I’m exporting) the system gives a huge random figure (15million or example) which will limit me being able to automate with it how I would like.

I believe I need to have a value template that states to not show anything when the figure is minus to correct this, but I can’t see how I do this? I tried to create a template but the system errored stating invalid option for MQTT?

This impacts at least my “Home instant Electricity” sensor and the “Home Instant Export” any help is very greatly received.

Edit: Home Instant shows a value of 15million, but export is showing nothing to clarify the impact.

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

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Electricity"
    unit_of_measurement: "kWh"
    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) }}"
    icon: "mdi:flash"
    device_class: energy

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Daily Electricity"
    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"
    device_class: energy

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Weekly Electricity"
    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"
    device_class: energy

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Monthly Electricity"
    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"
    device_class: energy

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Gas"
    unit_of_measurement: "m3"
    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:flash"
    device_class: gas

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Daily Gas"
    unit_of_measurement: "m3"
    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"
    device_class: gas
    state_class: 'total_increasing'

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Weekly Gas"
    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"
    device_class: gas
    state_class: 'total_increasing'

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Monthly Gas"
    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"
    device_class: gas
    state_class: 'total_increasing'

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Current Electricity Usage"
    unit_of_measurement: "kWh"
    value_template: "{{ value_json['elecMtr']['0702']['04']['00']|int(base=16) }}"
    icon: "mdi:speedometer"
    device_class: energy
    #state_class: 'total_increasing'

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Instant Export"
    unit_of_measurement: "kWh"
    value_template: "{{ value_json['elecMtr']['0702']['00']['01']|int(base=16) }}"
    icon: "mdi:flash"
    device_class: energy

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Electricity Export Meter"
    unit_of_measurement: "kWh"
    value_template: "{{ (value_json['elecMtr']['0702']['00']['01']|int(base=16) * value_json['elecMtr']['0702']['03']['01']|int(base=16) / value_json['elecMtr']['0702']['03']['02']|int(base=16)) }}"
    icon: "mdi:counter"
    device_class: energy
    state_class: total_increasing

  - state_topic: "SMART/HILD/[MAC]"
    name: "Home Gas Meter M3"
    last_reset_value_template: homeassistant.util.dt.utc_from_timestamp(0)
    unit_of_measurement: "m3"
    #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_increasing
    value_template: "{{ ( ( states('sensor.home_gas_meter_m3') | int ) * 1.02264 * 38.9 / 3.6 ) | round(1) }}"```

What do you want to do with negative values? For simple JSON such as…

{ "state": 1 }

…you can convert negative states to zero by using…

value_template: "{{ max(0, value_json.state) }}"

Alternatively you could use availability_template to make negatives unavailable.

Obviously this would have to be adapted for the very weird JSON you’re getting.

Apologies, I missed your reply.

So the ideal would be that the “Import” line reads as 0 when it gets to a minus number.

The export figure would ideally then “take over” and a (-) figure would be reflected in it (ideally as a positive).
Ill aim small and just try and get it to not be 16million watts first though.

Thank you

Fixed:

I updated the value_template to give me the valid figure (e.g. -16w) versus a multi-million watt version, this is for a SMETS1 SECURE 100 meter.

Updated config:

   value_template: >
      {% if value_json['elecMtr']['0702']['04']['00']|int(base=16) > 8388607  %}
      {{ value_json['elecMtr']['0702']['04']['00']|int(base=16) - 16777216 }}
      {% else  %}
      {{value_json['elecMtr']['0702']['04']['00']|int(base=16)}}
      {% endif %}   

Wish I had seen your post earlier. I was using a template based on proximity on low earth orbit satellites to set a custom deep sleep time on an ESP8266 . If proximity was too close (<2200km which could happen between seeing the distance at the start of an automation and calculating the new deep sleep time changed a little) I would get a negative number which would lock me out of my ESP with power on and drain the battery on the device.

{{ ((((states('proximity.home')|float-2200)**2 )**0.5)/6.5)|round(0)*1000 }}

My answer was to square the result and they square root it and so always get a positive number.

Rather than squaring and square-rooting, you could alternatively use abs to take the absolute value (ie. without any sign).

possibly_negative_value | abs
1 Like