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 %}
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.
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.
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!