Creating a good RESTable device

Hello,

I have my home running HA and a bunch of Arduinos for pumps, zones, reading the status of various systems.

I wrote custom software for them, I GET URLs from them and they express themselves speaking json and everything works with only minor glitches.

But there I would very much like to improve and simplify some things, first one being how am I talking to switches. I am using the command line with curl and jq, and I think this is a ton of overhead. For example:

switch:
  - platform: command_line
    switches:
        command_on: "/usr/bin/curl -s -X GET http://saladebombas:8080/EncenderPlanta?planta=2"
        command_off: "/usr/bin/curl -s -X GET http://saladebombas:8080/ApagarPlanta?planta=2"
        command_state: "/usr/bin/curl -s -X GET http://saladebombas:8080/Estado | jq '.bomba[2]'"
        value_template: '{{ value_json == "true" }}'
        friendly_name: Suelo planta segunda

I would like to move that to RESTful Switch. No problem, just a few changes and I’d be able to do something like this:

  - platform: rest
    resource: http://saladebombas:8080/Planta/2
        value_template: '{{ value_json == "true" }}'
        friendly_name: Suelo planta segunda

My question is, can I get the state of all the switches in one fell swoop? Poor old Arduinos do not have horsepower to spare, and I think it would be much kinder to them if there was a way to express a way to get with a single POST the value of all the switches that a particular Arduino has. Something like:

  - platform: rest
    resource: http://saladebombas:8080/Planta
        friendly_name: Suelos

Of course, any other comment on my plan or pointers would be appreciated as well.

Thanks.

You can grab a whole bunch of attributes in one go. Then break the attributes out to their own sensors using template sensors in Home Assistant.

As an alternative have you considered MQTT?

Rather than home assistant scanning your devices on a fixed interval with the rest sensor, instead the devices push updates to an MQTT broker (e.g. HA Mosquitto addon), only when something changes.

You can then subscribe to topics on the broker with mqtt sensors in Home Assistant and the broker will push out updates only when there are changes.

Push updates are much more efficient and timely than polling.

Hi, thanks for your input,

I currently grab a lot of sensors in one go from the Arduinos that only have sensors. I would like to do the same for the switches. But switches being switches, they also need to provide a button to change their state; and here is perhaps where I reveal how little do I understand of the underlaying model of HA.

I have not only considered MQTT but also wrote some proof of concept code for the Arduinos. Your opinion on this is appreciated; it makes me think that working a little more on that front is probably a good idea.