MQTT automation not working. Help!

Hi! I have a application sending MQTT messages to mosquitto, and an “mqtt multiplexer”-automation that is supposed to filter the configured messages and write back the right value to the right instance in MQTT. I have below a reduced snippet of my automation. The problem is that the value of alexandra/batteries/main/timeremaining is never updated. Why? The input value is in seconds, and I am trying to reduce it to hours by dividing it by 60 and then again by 60, and finally a round to remove any decimals. Can anyone spot what I have done wrong?
The incoming MQTT message looks like this:
signalk/delta {“context”:“vessels.urn:mrn:imo:mmsi:0123456789”,“updates”:[{“timestamp”:“2021-07-10T22:49:11.541Z”,"$source":“can0.72”,“values”:[{“path”:“electrical.batteries.1.capacity.timeRemaining”,“value”:695940}]}]}

The Automation:
service: mqtt.publish
data_template:
payload: >
{% if ‘stateOfCharge’ in trigger.payload %}
{{(trigger.payload_json[‘updates’][0][‘values’][0].value*100)|round(0)}}
{% elseif ‘capacity.timeRemaining’ in trigger.payload %}
{{(trigger.payload_json[‘updates’][0][‘values’][0].value/60/60)|round(0)}}
{% else %}
{{trigger.payload_json[‘updates’][0][‘values’][0].value|to_json}}
{% endif %}
topic: >
{% if ‘stateOfCharge’ in trigger.payload %}
alexandra/batteries/main/soc
{% elif capacity.timeRemaining’ in trigger.payload %}
alexandra/batteries/main/timeremaining
{% else %}
alexandra/error
{% endif %}

/A

First off, you need to use code blocks to format your code properly in your post.

second, you are using ‘elseif’ in the payload template. it should be ‘elif’.

then, in the topic template you are missing a single quote in the elif before ‘capacity.timeRemaining’

last, you don’t need to use “data_template”. just use “data”.

Sorry. The errors you are mentioning are due to minimizing the automation. Here is the full automation:
‘’’
service: mqtt.publish
data:
payload: |
{% if ‘tanks’ in trigger.payload %}
{{(trigger.payload_json[‘updates’][0][‘values’][0].value100)|round(0)}}
{% elif ‘capacity.timeRemaining’ in trigger.payload %}
{{(trigger.payload_json[‘updates’][0][‘values’][0].value/60/60)|round(0)}}
{% elif ‘stateOfCharge’ in trigger.payload %}
{{(trigger.payload_json[‘updates’][0][‘values’][0].value
100)|round(0)}}
{% elif ‘pressure’ in trigger.payload %}
{{(trigger.payload_json[‘updates’][0][‘values’][0].value/100)|round(0)}}
{% elif ‘temperature’ in trigger.payload %}
{{(trigger.payload_json[‘updates’][0][‘values’][0].value-272.15)|round(2)}}
{% else %}
{{trigger.payload_json[‘updates’][0][‘values’][0].value|to_json}}
{% endif %}
topic: |
{% if ‘tanks.freshWater.1.currentLevel’ in trigger.payload %}
alexandra/sensor/tanks/freshwater/level
{% elif ‘capacity.timeRemaining’ in trigger.payload %}
alexandra/batteries/main/timeremaining
{% elif ‘stateOfCharge’ in trigger.payload %}
alexandra/batteries/main/soc
{% elif ‘tanks.blackWater.0.currentLevel’ in trigger.payload %}
alexandra/sensor/tanks/blackwater/level
{% elif ‘tanks.fuel.0.currentLevel’ in trigger.payload %}
alexandra/sensor/tanks/fuel/level
{% elif ‘navigation.position’ in trigger.payload %}
alexandra/position
{% elif ‘environment.outside.pressure’ in trigger.payload %}
alexandra/environment/outside/pressure
{% elif ‘electrical.batteries.1.current’ in trigger.payload %}
alexandra/batteries/main/current
{% elif ‘electrical.batteries.1.voltage’ in trigger.payload %}
alexandra/batteries/main/voltage
{% elif ‘electrical.batteries.2.voltage’ in trigger.payload %}
alexandra/batteries/start/voltage
{% elif ‘environment.inside.refrigerator.temperature’ in trigger.payload %}
alexandra/environment/inside/refridgerator/temperature
{% elif ‘environment.inside.maincabin.temperature’ in trigger.payload %}
alexandra/environment/inside/maincabin/temperature
{% elif ‘environment.inside.engineroom.temperature’ in trigger.payload %}
alexandra/environment/inside/engineroom/temperature
{% else %}
alexandra/error
{% endif %}
‘’’

you still didn’t format your code properly.

right now it’s an unformatted wall of text.

What do you mean it’s not working? errors in the validation check? errors in the log?

Have you tested the templates in the template editor?

Sorry about that. I tried to format the code as recommended, but it only seems to work for people using my locale. (For me it looks OK)
Anyway, My issue seems to be non-existent as long as the computed result is a whole number without decimals. I guess it is some locale issue. Here in Sweden we use “,” as the decimal delimiter, and the incoming MQTT data seems to use “.” as delimiter.

Put three back ticks ``` on their own line above and below your pasted code.

That is exactly what I did. Second try:

service: mqtt.publish
data:
  payload: |
    {% if 'tanks' in trigger.payload %}
      {{(trigger.payload_json['updates'][0]['values'][0].value*100)|round(0)}}
    {% elif 'capacity.timeRemaining' in trigger.payload %}
      {{(trigger.payload_json['updates'][0]['values'][0].value/60/60)|round(0)}}
    {% elif 'stateOfCharge' in trigger.payload %}
      {{(trigger.payload_json['updates'][0]['values'][0].value*100)|round(0)}}
    {% elif 'pressure' in trigger.payload %}
      {{(trigger.payload_json['updates'][0]['values'][0].value/100)|round(0)}}
    {% elif 'temperature' in trigger.payload %}
      {{(trigger.payload_json['updates'][0]['values'][0].value-272.15)|round(0)}}
    {% else %}
      {{trigger.payload_json['updates'][0]['values'][0].value|to_json}}
    {% endif %}
  topic: |
    {% if 'tanks.freshWater.1.currentLevel' in trigger.payload %}
      alexandra/sensor/tanks/freshwater/level
    {% elif 'capacity.timeRemaining' in trigger.payload %}
      alexandra/batteries/main/timeremaining
    {% elif 'stateOfCharge' in trigger.payload %}
      alexandra/batteries/main/soc
    {% elif 'tanks.blackWater.0.currentLevel' in trigger.payload %}
      alexandra/sensor/tanks/blackwater/level
    {% elif 'tanks.fuel.0.currentLevel' in trigger.payload %}
      alexandra/sensor/tanks/fuel/level
    {% elif 'navigation.position' in trigger.payload %}
      alexandra/position
    {% elif 'environment.outside.pressure' in trigger.payload %}
      alexandra/environment/outside/pressure
    {% elif 'electrical.batteries.1.current' in trigger.payload %}
      alexandra/batteries/main/current
    {% elif 'electrical.batteries.1.voltage' in trigger.payload %}
      alexandra/batteries/main/voltage
    {% elif 'electrical.batteries.2.voltage' in trigger.payload %}
      alexandra/batteries/start/voltage
    {% elif 'environment.inside.refrigerator.temperature' in trigger.payload %}
      alexandra/environment/inside/refridgerator/temperature
    {% elif 'environment.inside.maincabin.temperature' in trigger.payload %}
      alexandra/environment/inside/maincabin/temperature
    {% elif 'environment.inside.engineroom.temperature' in trigger.payload %}
      alexandra/environment/inside/engineroom/temperature
    {% else %}
      alexandra/error
    {% endif %}
1 Like

Worked that time.

You can use |replace(',', '.') to replace comas with periods in your published message.

Also you should use > as the multi-line template delimiter, not |. I have seen this cause issues in the past.

Good. Had to copy your example as I didn’t find the correct character on my keyboard. Tried your suggestion, but it didn’t work. Just before your suggestion, time remaining went OK, but cabin temperature value got value “unknown”. Tried your suggestion, but the result is the same. Pretty sure the problem is caused by the delimiter.

I think home assistant may be interpreting the coma as a list. Try this
|replace(', ','.')|replace('(','')|replace(')','')

Turns out that the problem must have been some garbage character in the code. I re-wrote the whole script manually, and now it seems to be working. Thanks for your help!

1 Like