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:
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.
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.