aREST Function gets automatically called every 30 seconds

Hi, I have an aREST endpoint running on a Node JS Server. I wrote a simple test html which calls my aREST function to make sure it works, and it does.

But when I include the aREST component in Home Assistant and declare a switch with my function, this functions gets called every 30 seconds automatically after restarting HA, which is super annoying since it toggles a light switch.

switch:
  - platform: arest
    name: Controllinator 3000
    resource: http://my_server_ip:3000
    functions:
      lightSwitch:
        name: Light Switch

After every automatic call, I get an error in the log:
[homeassistant.helpers.entity] Update for switch.controllinator_3000_light fails

There is also a stracktrace which I cannot post at the moment, since I currently have no access to my HA instance. If you need it, please let me know.

If I add pins instead of functions, it seems to work.
Do you have any ideas to make the functions work correctly?

Home Assistant will poll the endpoint to determine the state and upstate the switch accordingly. You will have to check if the GET-parameter params is set or not to determine if you should take action or return state.

https://github.com/home-assistant/home-assistant/blob/a0067a298ac89b98e6baad536460abc22c6e3826/homeassistant/components/arest/switch.py#L133

Thanks for your answer. Do you have any example? I am not able to make it work :frowning:

My function in NodeJS using the pi-aREST module looks like this:

var piREST = require('pi-arest')(app);

function lightSwitch(value, callback) {
    piREST.digitalWrite(29, 1);
    if (callback != null) {
       callback(1);
    }
}

piREST.function("lightSwitch", lightSwitch);

When HA calls this function, the callback is undefined and the value contains:

function(result) {
  answer.return_value = result;
  res.json(answer);
}

I have no idea what I can do with this information.

/edit: Ok, I called the URL via curl to find out what’s inside my value variable
curl -X GET http://my_url:3000/lightSwitch gives me the function code from above
curl -X GET http://my_url:3000/lightSwitch?params=1 give me 1

So I changed my code to the following:

function lightSwitch(value, callback) {
    if (value == 0 || value == 1) {
        piREST.digitalWrite(29, 0);
        if (callback !== undefined) {
            callback(1);
        }
    }
    else {
        console.log("what now?");
    }
}

The call with ?params toggles the light, the call without ?params does nothing … but HA cannot configure the switch since it runs in a timeout

File "/usr/src/homeassistant/homeassistant/components/arest/switch.py", line 117, in __init__
    request = requests.get(f"{self._resource}/{self._func}", timeout=10)

Setup of switch platform arest is taking over 10 seconds.

So, in my understanding, my else branch must return anything to signify HA that the switch is alive. But what?

This part is not my strong suit, but you are supposed to return the current state of the switch in json. Format should be something along the line of:

{"return_value": 1}

Use 1 for on and 0 for off.

I just found out, that I can call the function which is part of the value variable, so I just added the following code

if (callback !== undefined) {
        callback(1);
    }
    else if (typeof value === "function") {
        value(1);
    }

For HA (or any curl), the value holds the function

function(result) {
  answer.return_value = result;
  res.json(answer);
}

which creates a json response.

It works, but I am not sure if this is the correct way.

Sounds reasonable. Simple way to verify if it works is to change the switch state from outside of home assistant (e.g. with curl) and check if Home Assistant updates the state correctly (within the scan interval).

My problem is that I currently have no possibility to get the state of the light switch from my NodeJS server, so I return always 1 as state.
Since the light switch can also be pressed manually, I have no idea if the light is currently on or off.
That’s why I use this switch as a simple button instead of a toggle switch that show the on/off state.