Help needed with value_template, please

Hi,

I have a MQTT topic that is filled with data coming from my weather station.
One of the json entries is:

    "battery_ok": 1
    ...

The initial syntax I used to display this is (in a file …/mqtt/binary_sensors.yaml) :

- name: "Weerstation batterij"
  unique_id: "weather_battery"
  force_update: true
  payload_on: 1
  payload_off: 0
  state_topic: "rtl_433/Vevor-7in1/11564"
  value_template: "{{ value_json.battery_ok }}"

For some reason this value (1) is being displayed as “On”
I wanted to show this as OK, or when value is 0, as “Low”, so I started to fiddle with the value_template.
Anything I do, however, changes the display to either Unavailable or Unknown.

What I tried:

...
value_template: >
  {% if value_json.battery_ok == 1 %}
      OK
    {% else %}
      LOW
    {% endif %}

Does not work.
I tried changing == 1 to == ‘1’, but that isn’t working either.
Also tried changing == 1 to == INT 1, but no cigar.

So my questions are:

  1. Why is the value 1 being displayed as On ?
  2. What am I doing wrong in the template manipulation ?

Thanks!

The state of a binary sensor is on or off. The front-end view can be modified by setting a device_class.

Read this page: Binary sensor - Home Assistant

To show custom values, you will need to set up a sensor, not a binary sensor.

Thanks,

This will get me going in the right direction, I think.
One thing though, how does HA know it is a binary sensor, or thinks this is a binary sensor ?
Probably not because the code is in a file called binary_sensors.yaml, right ?

Yes. You probably have a file sensors.yaml too ? So move it.

Probably not… :wink:

you should have somewhere in your config a yaml key that tells HA that the file you are referencing (“/mqtt/binary_sensors.yaml”) contains binary_sensors.

the usual config for mqtt binary sensors that aren’t split out from the configuration.yamnl is (per the docs):

mqtt:
  - binary_sensor:
      state_topic: etc....

if you split it out you still need to have at least the “mqtt:” followed by an !include to tell HA where to find the files.

Since we can’t see your config you will need to figure out how you split the config and look for “binary_sensor:” somewhere in there.

Thank you!

This of course the issue…
Had forgotten completely about the settings in the configuration.yaml file:

mqtt:
  - sensor: !include mqtt/sensor.yaml
  - binary_sensor: !include mqtt/binary_sensors.yaml

Still learning…
I will change the config and I’m sure the issue will be resolved.

Thanks again!

1 Like

Hay bro

this is what I did.

mqtt:
  sensor: !include_dir_merge_list mqtt/sensor/
  climate: !include_dir_merge_list mqtt/climate/
  binary_sensor: !include_dir_merge_list mqtt/binary_sensor/
  switch: !include_dir_merge_list mqtt/switch/
  light: !include_dir_merge_list mqtt/light/
  fan: !include_dir_merge_list mqtt/fan/
  device_tracker: !include_dir_merge_list mqtt/tracker/

!include_dir_merge_list
this lets you put heaps of file in the folder

them the file look like

just watch out the indenting can give you a lot of trouble.