MQTT JSON- Boolean: How to map to 0 and 1?

Hi,

I have some sensors which are updated through MQTT. Some of them have boolean values as follows:

  "status": {
    "daemon": true,
    "manual": false,
    "nacht": false,
    "test": false,
    "heizung": true

To get this values into Grafana I would like to map them as “0” and “1” (from “false” and “true”).

I tried with the template but did not succeed due to some syntax error I do not know how to resolve:

- platform: mqtt
   state_topic: hzg/status
   name: "daemon"
   force_update: true
   value_template: '{{ value_json.status.daemon | regex_replace(find='true', replace='1') | regex_replace(find='false', replace='0')}}'

The editor shows me:

can not read an implicit mapping pair; a colon is missed at line 58, column 135:
     ... e(find='false', replace='0') }}'
                                         ^

Anyone having an idea how to convert this properly?

Thanks!

/KNEBB

Something like:


"{% if value_json.staus.daemon == 'true' %} 1 {% else %} 0 {% endif %}"

should work.

Ok, now I have the issue of not being sure whre and how to insert your code.

value_template: '{{% if value_json.relais.r_wp == 'true' %} 1 {% else %} 0 {% endif %}}'

Does not work (getting sysntax errors).

Sorry, syntax of these entries is not self-explaining…

Thanks!

/KNEBB

If you are using single quotes within the template you must use double quotes at either end of the template

value_template: "{{% if value_json.relais.r_wp == 'true' %} 1 {% else %} 0 {% endif %}}"

1 Like

Sorry to bother you… but using the suggested sniplet results in:

invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data['value_template']. Got "{{% if value_json.relais.r_wp == 'true' %} 1 {% else %} 0 {% endif %}}". (See ?, line ?). 

My config:

 - platform: mqtt
   state_topic: hzg/status
   name: "r_wp"
   force_update: true
   value_template: "{{% if value_json.relais.r_wp == 'true' %} 1 {% else %} 0 {% endif %}}"

I tried either the other way with single/ double quotes. No change.

You have double curly brackets at either end of the template.
You only want single curly brackets.

You want

value_template: "{% if value_json.relais.r_wp == 'true' %} 1 {% else %} 0 {% endif %}"

:+1::+1:

I really, really want to understand syntax here. But it looks like not even you experienced (and very helpful) lads are really sure how it work.

However, your last suggestion still does not work:

Invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: unexpected char '‘' at 33) for dictionary value @ data['value_template']. Got '“{% if value_json.relais.r_wp == ‘true’ %} 1 {% else %} 0 {% endif %}”'. (See ?, line ?). 
 - platform: mqtt
   state_topic: hzg/status
   name: "r_wp"
   force_update: true
   value_template: “{% if value_json.relais.r_wp == ‘true’ %} 1 {% else %} 0 {% endif %}”

/KNEBB

value_template: >
  {% if value_json.relais.r_wp == 'true' %} 
    1 
  {% else %} 
    0 
  {% endif %}

As shown in Template - Home Assistant

Edit:

@knebb In HA you have the Developer Tools and there is a template debug there.
Just insert whatever template you want to test and see at once if it gives a value or error.

Nevermind.

This time it was a simple cut&paste error.
The ’ where replaced by backticks. :frowning:

At least I do not have syntax errors right now. I will monitor to see if the value gets properly populated to Grafana.

Thanks so far!

/KNEBB

Last try :wink:

Remove the single quotes around true in your template.

value_template: “{% if value_json.relais.r_wp == true %} 1 {% else %} 0 {% endif %}”

1 Like

To convert boolean true/false to integer 1/0 simply use the abs filter (absolute).

  - platform: mqtt
    state_topic: hzg/status
    name: "daemon"
    force_update: true
    value_template: '{{ value_json.status.daemon | abs }}'

This won’t work if the value is string true/false.

Hi @123 :frowning:
No, they are indeed boolean, not strings:

"relais": {
    "r_wp": true,
    "r_ehzg": false,
    "r_pump": true,
    "r_lhzr": 95
  },

When I used the suggested " then the result is a “0” as string- which is not good for Grafana.
So I used the single one outside and the double around true.
Now it works. I do not know what the abs function will do but I am happy how it is currently.

value_template: '{% if value_json.relais.r_wp == "true" %} 1 {% else %} 0 {% endif %}'

Thanks for all input!

/KNEBB

Last update:

For some reason the %if% %else% version did not work and always produced the value 0.

I used the second solution with abs and this now finally works fine!

/KNEBB

If the received value is not a string, then it failed to work because it was being compared to a string.

'{% if value_json.relais.r_wp == "true" %} 1 {% else %} 0 {% endif %}
                                 ^^^^^^
                          The double-quotes
                          make this a string

Either remove the double-quotes or just use abs.

{{ value_json.relais.r_wp | abs }}

Oh guys, HoAss is really complicated…

I guess I am understanding finally. At least partially.

So both of the following should work without issues:

value_template: '{% if value_json.relais.r_wp == true %} 1 {% else %} 0 {% endif %}'

value_template: '{{ value_json.status.daemon | abs }}'

Finally…

Glad to hear it. However, it also means the post you marked as the Solution is now no longer the correct solution because it compares the value to a string true.

Please consider marking my post above with the Solution tag so other users are informed of the correct way to solve this issue.