Template Binary Sensor - Help - solved

Help, I have spent about 2 hours trying to get this to work, but keep getting “invalid config” can anyone help?
Home Assistant: 0.39.2

State we want to make into a binary sensor so we can apply a device_class to it:
Entity: “rflink.connection_status”
State: “connected”

    binary_sensor:
      - platform: template
        sensors:
          example_binary_sensor:
             friendly_name: 'RFlink Gateway Status'
             device_class: 'connectivity'
             value_template: **(I have tried the following)**
             value_template: '{% if is_state("rflink.connection_status", "connected") %}on{% else %}off{% endif %}'
             value_template: '{{ states.rflink.connection_status.state == "connected"}}'
             value_template: "{{ rflink.connection_status == 'connected' }}"
             value_template: '{% if is_state("rflink.connection_status", "connected") %}on{% else %}off{% endif %}'
             value_template: "{% if states.rflink.connection_status.state == "connected") %}on{% else %}off{% endif %}"

value_template: "{% if is_state('rflink.connection_status', 'connected') %}on{% else %}off{% endif %}"

Is what you are looking for I think.

Thank you so much. I was getting really worked up about it.

I found I needed to make one small change, swapped the on/off for true/false, else the binary sensor ignored it.

Working code reference

binary_sensor:
 - platform: template
   sensors:
     rflink_status:
       value_template: "{% if is_state('rflink.connection_status', 'connected') %}true{% else %}false{% endif %}"

I had the same issues with templates. Now I test everything in the template tester in the dev states panel for a quicker back and forth on what may be missing or incorrect.

But it’s always easy to reverse the single and double quotes in YAML until you get used to it. I’ll bet you don’t do it again!

Glad I could help.

I’m still new to HASS and can’t really get my head around the rules on single and double quotes, many of the examples seem to use them interchangeably. Is there a ruleset?

Many thanks for your help, in case anyone comes accross this, the finished code can be found here: https://github.com/Genestealer/Home-Assistant-Configuration/blob/master/includes/binary_sensors/gateways.yaml

1 Like

I was going to say its weird some of your lines use single outside quotes for the line while others use double. But as you can tell either work fine, if you use single on the outsides then everything inside will have to use double quotes, and vise versa, I suggest sticking to one style though.

I don’t know about actual rulesets, but personally when it comes to template sensors I always use double quotes outside. I feel like if you use double quotes outside, then you always use singles inside.

1 Like