One response for multiple sensors

I’m using this configuration to read 4 TCP sensors:

sensor:
  - platform: command_line
    name: hhc_input1
    scan_interval: 1
    command: echo -n input | timeout 0.2 nc 192.168.0.105 5000           
    value_template: "{{ value[22:23] == '0' }}"

  - platform: command_line
    name: hhc_input2
    scan_interval: 1
    command: echo -n input | timeout 0.2 nc 192.168.0.105 5000           
    value_template: "{{ value[21:22] == '0' }}"

  - platform: command_line
    name: hhc_input3
    scan_interval: 1
    command: echo -n input | timeout 0.2 nc 192.168.0.105 5000           
    value_template: "{{ value[20:21] == '0' }}"

  - platform: command_line
    name: hhc_input4
    scan_interval: 1
    command: echo -n input | timeout 0.2 nc 192.168.0.105 5000           
    value_template: "{{ value[19:20] == '0' }}"

It doesn’t work because with 1 second scan_interval the various calls overlap and the server often doesn’t answer. But each answer contains the status of all sensors (eight, actually).

How to merge all these value_template in only one command_line call, that retrieve the status of the 8 sensors at once? I’m thinking about create a “virtual sensor” (a variable) that contains the answer of the server and then use it to feed the value_templates… but I’m not sure how to do with the HA components.

Assuming these are on/off sensors, i.e. 0 or 1… What’s odd to me is that you evaluate 0 equaling true but not 1. Anways, with that in mind…

sensor:
  - platform: command_line
    name: hhc_inputs
    scan_interval: 1
    command: echo -n input | timeout 0.2 nc 192.168.0.105 5000           
    value_template: "{{ value[15:23] }}"      
binary_sensor:
  - platform: template
    sensors:
      htc_input1:
        value_template: "{{ states('sensor.hhc_inputs')[0] == '0' }}"
      htc_input2:
        value_template: "{{ states('sensor.hhc_inputs')[1] == '0' }}"
      htc_input3:
        value_template: "{{ states('sensor.hhc_inputs')[2] == '0' }}"
      htc_input4:
        value_template: "{{ states('sensor.hhc_inputs')[3] == '0' }}"
      htc_input5:
        value_template: "{{ states('sensor.hhc_inputs')[4] == '0' }}"
      htc_input6:
        value_template: "{{ states('sensor.hhc_inputs')[5] == '0' }}"
      htc_input7:
        value_template: "{{ states('sensor.hhc_inputs')[6] == '0' }}"
      htc_input8:
        value_template: "{{ states('sensor.hhc_inputs')[7] == '0' }}"

Only the sensor will ping your hardware. The binary sensors will update whenever sensor.hhc_inputs changes.

1 Like

Great! Just a question: why you have to equal the input value to ‘0’ in sensor? It’s not enough to assign the raw value? Something like:

value_template: "{{ value[15:23] }}"

About the 0/1 question: because I prefer to use nc contacts I had to invert their logical state. I.e. when the switch is ‘off’ it actually close the input on the board to 0V.

You can assign the raw values, but then you’d need to put it in a sensor, not a binary_sensor. Binary sensors are easier to manage if the result is 1 or 0. That way when you reference the binary sensor, you can just see if its ‘on’ or ‘off’. And you can add an appropriate device_class. For example, if the 3rd one is a door, add device_class: door and it will return open/close.

Sorry, perhaps I didn’t explained well. I’m talking about the first sensor assignment:

sensor:
  - platform: command_line
    name: hhc_inputs
    scan_interval: 1
    command: echo -n input | timeout 0.2 nc 192.168.0.105 5000           
    value_template: "{{ value[15:23] == '0' }}"      

The condition is always false: the string value[15:23] will never be equal to the character '0'.
Naively, I thought the value_template for the sensor should be just value[15:23], while for the binary_sensors below it’s reasonable that each one would compare the related byte.

Ah yes, that was a mistake. It should just be

sensor:
  - platform: command_line
    name: hhc_inputs
    scan_interval: 1
    command: echo -n input | timeout 0.2 nc 192.168.0.105 5000           
    value_template: "{{ value[15:23] }}"     
1 Like