Node-RED Inverts Binary Sensors?

I have just come across a weird issue with using template binary sensors.

I have input_boolean.test and created a binary sensor off of the input boolean. In lovelace, when I turn the boolean off, the sensor says off, when I turn the boolean on, the sensor says on. However, when I go to use it in an automation, the binary sensor is reversed???

 - platform: template
   sensors:
      test_binary_sensor:
        friendly_name: Test Binary Sensor
        icon_template: >
          {% if is_state('binary_sensor.test_binary_sensor', 'on') %}
            mdi:trash-can
          {% else %}
            mdi:truck
          {% endif %}
        value_template: "{{ is_state('input_boolean.test', 'on') }}"

You’re referencing itself in the template. This means its pulling the state from the state machine, which is the previous state for your sensor. Change your icon template to use the input_boolean.

 - platform: template
   sensors:
      test_binary_sensor:
        friendly_name: Test Binary Sensor
        icon_template: >
          {% if is_state('input_boolean.test', 'on') %}
            mdi:trash-can
          {% else %}
            mdi:truck
          {% endif %}
        value_template: "{{ is_state('input_boolean.test', 'on') }}"

I did that, but I am getting the same behavior as before.

Sorry but you should list node red next time. I’ll change this to third party.

I didnt think it was a node-red issue, as i have the same behavior when I use the built in automation editor. Thank you though.

I have noticed that if you use call state node too soon after a state change then it will get the previous state.

I have, with a crude setup, concluded that it’s more than 200 ms and less than 500 ms needed to get a correct value.
I just stopped at 500 because it doesn’t really matter.

1 Like

That is interesting. Does not explain why the input_boolean says on and the binary_sensor says off though

I can 100% without uncertainty tell you that this is not home assistant or your template sensor. This is an issue between node red and home assistant.

Okay you are right, it is 100% a Node-RED issue.