Conditional REST polling

I’m looking into a way of conditionally polling a sensor and updating an input_number with the received value.

Being a conditional action, I’d use automation. But invoking the rest_command from automation on a time_pattern trigger (every X mins) in case a condition is met, I cannot access the value returned by the rest_command to update an input_number via its set_value service.

Use case: if the TV is accessible on the local network, syncronize its volume value with an input_number (the TV being controllable via both remote and HA).

Any help would be appreciated.

Use a restful sensor to get your tv volume. Set an appropriate scan_interval.

For the automation that synchronises the input_number, don’t trigger on time. Trigger on change of the restful sensor state.

trigger:
  platform: state
  entity_id: sensor.tv_volume
condition:
   condition: template
   value_template: "{{ states('sensor.tv_volume') != states(input_number.tv_volume') }}"
action:
<set input_number = sensor.tv_volume>

Use another automation that triggers on change of the input number to call the restful command to set the volume. This will also trigger the first automation, but will not get past the condition.

Thanks for the quick answer. Currently I’m doing what you’ve suggested… but it’s not elegant. It’s more like a workaround.

1 Like

Ok I’ll be interested to see other solutions as it is the only one I know.

In the end I gave up on this approach, however having a sensor’s scan interval increased to one day or more and triggering its update with homeassistant.entity_update service when the condition on a different sensor’s state is met would have worked if the platform had the right service defined (e.g. poll for TV’s volume value only if the TV is turned on). It did not have.

I moved everything under a command_line sensor that polls for the TV’s state and if it’s on do further polls on volume, channel, etc.
It’s a one-liner like this below, a one long liner :slight_smile:

(ha_venv3_8) mypi@raspberrypi:~/homeassistant_conf $grep -v "^#" sensors/sharp_tv_attributes.yaml | grep command
platform: command_line
command: "in=2;ch=15;mute=2;vol=0;up=$(curl -m 60 -s http://192.168.1.122:5000/?serial=POWR?|sed 's/\\r//');[ ${up} -eq 1 ] && vol=$(curl -m 60 -s http://192.168.1.122:5000/?serial=VOLM??|sed 's/\\r//') && ch=$(curl -m 60 -s http://192.168.1.122:5000/?serial=DTVD???|sed 's/\\r//') && mute=$(curl -m 60 -s http://192.168.1.122:5000/?serial=MUTE?|sed 's/\\r//') && in=$(curl -m 60 -s http://192.168.1.122:5000/?serial=IAVD?|sed 's/\\r//'); echo \"{\\\"tv_up\\\":\\\"$up\\\",\\\"volume\\\":\\\"$vol\\\",\\\"mute\\\":\\\"$mute\\\",\\\"channel\\\":\\\"$ch\\\",\\\"input\\\":\\\"$in\\\"}\""
(ha_venv3_8) mypi@raspberrypi:~/homeassistant_conf $

1 Like