Two ways communication between HA and Node-Red

Hi.

I wanted to create a switch in HA that would cause an action in Node Red which would the report the status of the switch.

Node-Red supports functionalities with a Unifi UDM Pro that unfortunately the native HA integration doesn (traffic rules). So I created a switch node, which can either turn on or off the traffic rule on the UDM Pro (blocking the internet of my respective kids). To report the status of the traffic rule, it then sends the payload to a binary sensor node.

In HA, this creates two entities: a switch and a binary sensor.
To combine the two, I had to create a switch template that uses the value of the binary sensor for the status, and send the on / off command to the switch.

- platform: template
  switches:
    hannah_internet:
      friendly_name: "Hannah Internet"
      unique_id: "hannah_internet"
      value_template: "{{ is_state('binary_sensor.hannah_internet_rule', 'on') }}"
      turn_on:
        service: switch.turn_off
        target:
          entity_id: switch.hannah_internet_rule
      turn_off:
        service: switch.turn_on
        target:
          entity_id: switch.hannah_internet_rule

I’m wondering if that’s the right way to go about it ?
Only using a single HA node in Node-RED that would allow to send a command from HA to Node and get the status of the operation back to HA.

TIA
Jean-Yves