Update state of a input_boolean from http GET

I have well working DIN relay and temperature control system in my cottage called the Webswitch 1216H ( http://www.webswitch.se/ ) which I would like to integrate into HA. The APIs are quite rudimentary and are just using authenticated http GET commands and no true REST/JSON.

I have been able to integrate the temperature reading with very simple REST sensors:

sensor:
  - platform: rest
    resource: http://[Webswitch IP]/temperature/get2/1
    unit_of_measurement: "°C"

And thanks to this thread I have a working relay control:

However, what I’m lacking is the state update if the relay is changed from another source (physical switch).

In short, it utilizes three http GET commands:

ON:
http://[Webswitch IP]/relaycontrol/on/1

OFF:
http://[Webswitch IP]/relaycontrol/off/1

and state:

http://[Webswitch IP]/relaystate/get2/1

With the output 1 for ON and 0 for OFF

To get the control working, this is my yaml entries:

sensor:
  - platform: rest
    resource: http://192.168.0.10/relaystate/get2/1
    name: torso_heat_state

rest_command:
  torso_heat_on:
    url: http://192.168.0.10/relaycontrol/on/1
    method: GET

  torso_heat_off:
    url: http://192.168.0.10/relaycontrol/off/1
    method: GET

input_boolean:
  torso_heat:
    name: Torsö värme

automation:
  - alias: 'Heat control'
    trigger:
      platform: state
      entity_id: input_boolean.torso_heat
    action:
      service_template: rest_command.torso_heat_{{trigger.to_state.state}}

What works is that if I trigger the input_boolean from a button this work as expected and the sensor “torso_heat_state” updates correctly to 1 if turned ON and 0 if turned OFF. This is also updated correctly if the relay is changed remotely. However, what I would like it that the input_boolean also is updated from the “torso_heat_state” sensor.

Can anyone help me with a solution to this?

Thanks’

I’m not 100% here, but this sounds like you’re trying to make your own switch.

Which is what a Template Switch does.

# Example configuration.yaml entry
switch:
  - platform: template
    switches:
      torso_heat:
        # State follows the Rest State
        # Make it so if that whatever the 'on' state is checked here. 
        # This switch will be 'on' if this value_template evaluates 
        # to 'true'. Off otherwise. 
        value_template: "{{ is_state('sensor.torso_heat_state', 'on') }}"
        turn_on:
          service: rest_command.torso_heat_on
        turn_off:
          service: rest_command.torso_heat_off

Now you have a switch that also follows the state of the sensor. So if the sensor changes state from any other source, the switch will turn it self on/off to match. The rate at which the switch syncs with the heater would be the polling interval of your rest sensor, so don’t expect it to update immediately.


Actually, if you want it to update quicker, there is a function you can call

homeassistant.update_entity

So, create a script that will send the rest command and force an update.

# Example configuration.yaml entry
switch:
  - platform: template
    switches:
      torso_heat:
        # State follows the Rest State
        # Make it so if that whatever the 'on' state is checked here. 
        # This switch will be 'on' if this value_template evaluates 
        # to 'true'. Off otherwise. 
        value_template: "{{ is_state('sensor.torso_heat_state', 'on') }}"
        turn_on:
          service: script.torso_heat
          data:
             action: on
        turn_off:
          service: script.torso_heat
          data:
            action: off

script:
  # Sends an on/off command and forces a state update to the sensor rather
  # than waiting for the next polling interval. 
  torso_heat:
    sequence:
      # The script requires an action of 'on' or 'off' data to be passed in.
      - service_template: "rest_command.torso_heat_{{action}}"
      # Give it a second to update state
      - delay: "00:00:01"
      # Force an auto update of the sensor state now that we've changed states. 
      - service: homeassistant.update_entity
        entity_id: sensor.torso_heat_state
      

Thank you, Jim! This was super helpful. I was initially trying to implement this as a switch but got sidetracked by the post i linked to above.

The first version you propose works well but has, as you mention the delay until the next polling.

However, I have a challenge getting the script to work. I get the error message:
Service rest_command.torso_heat_ does not match format <domain>.<name>

It indicates that the {{action}} is not passed on at the service_template stage. This is probably very obvious, but I’m just moving over to HA from OpenHAB so I’m a bit of a newbie in HA.

Thank you again!

Now I spotted the error.

At least in my setting, I needed double quotes around the on and off action i.e.,

turn_on:
          service: script.torso_heat
          data:
             action: "on"
        turn_off:
          service: script.torso_heat
          data:
            action: "off"

I’m also no so patient, so I changed the script delay to:

      - delay:
            milliseconds: 200

Now it is working just as it should. Thank you again for the excellent help