Creating rest switch difficulties

Hi you all.
New to HA, all standard stuff OK.
But now I want to send a http request from a lovelace card like “yun.local/arduino/on or yun.local/arduino/off” and I’m stuck and frustrated.
Tried to read everything and tried all examples; I’m doing something (or a lot) wrong but do not have any success.
Is there someone here to help me?
Thanks in advance!

Maybe I should clarify the problem a bit.

I have a piece of equipment running which switches relays off and on during a timed run which is initiated by sending “on” or “off”.
That is no problem using curl etc.
During the sequence it sends status texts eg “running”, “emptying”, “filling” etc.
Those text and the format I can ajust to what is needed for HA to store and show them on my lovelace card.
So to be short I want to PUSH status text over http TO HA, but when I search for any example I only get examples of pushing messages to phones FROM HA and that’s not what I’m after.
Anyone can help with this?
To be clear I want to have the initiative of sending data to HA done by the equipment; not by polling by HA…

use a command_line sensor and send the curl command.

Thanks for your reply but I do not want HA to poll but have the initiative by the equipment sending eg. http://yun.local/arduino/running TO HA and showing in card as status.

set the scan_interval to some ridiculous high value. I have many such REST and command line sensors and I set those to like 360000 seconds.

Do you have some yaml example for that since everything I tried does not seem to work.
Is what you suggest updating the status immediately?

Here is a sensor that gets every Pokemon’s name … of course this rarely changes and as such there is no need to execute more than once at boot time:

- platform: rest
  name: Pokedex
  scan_interval: 360000
  unique_id: sensor.pokedex
  resource_template: https://pokeapi.co/api/v2/pokemon/?limit=1008
  value_template: "{{ now() }}"
  json_attributes:
    - results

360000 = 100 hours, could be higher but for that sensor I do not care

Here is another. This gets a random or targeted Pokemon. Since the kids hit it a few times a day, the scan interval again at 100 hours is fine. It even controls the URL hit based on the state of some input_select

- platform: rest
  name: Random Pokemon
  unique_id: sensor.random_pokemon
  scan_interval: 360000
  resource_template: >
      {% if states('input_select.select_pokemon_mode') == 'Increment' %}
        https://pokeapi.co/api/v2/pokemon/{{ state_attr('sensor.random_pokemon','id') + 1 }}
      {% elif states('input_select.select_pokemon_mode') == 'Decrement' %}
        https://pokeapi.co/api/v2/pokemon/{{ state_attr('sensor.random_pokemon','id') - 1 }}
      {% elif states('input_select.select_pokemon_mode') == 'Name' %}
        https://pokeapi.co/api/v2/pokemon/{{ states('input_select.pokedex') }}
      {% else %}
        https://pokeapi.co/api/v2/pokemon/{{ range(1, 1008) | random }}
      {% endif %}
  value_template: "{{ value_json.name }}"
  json_attributes:
    - abilities
    - base_experience
    - forms
    - game_indicies
    - height
    - held_items
    - id
    - is_default
    - location_area_encounters
    - moves
    - name
    - order
    - past_types
    - species
    - sprites
    - stats
    - types
    - weight

So this updates only when it is called, but in my situation I have to send text messages to HA evry once in a while as long the sequence is running, so one call won’t do the job I guess.
That’s why I think my equipment must send data to HA…

Aha, I think it’s sinking in…
Going to try some out and let you know.
Thanks a lot for now!

Then you do not want a high scan_interval. It is not clear what you are trying to accomplish. Is what you are asking if I only want to poll while something else is on/off then you could write something like the second example and set a low polling rate.

Have a boolean on/off and when “on” hit the URL that returns some status, when it is off hit a URL that returns something like “idle”. If it isn’t much of a load on the system, that could be the same URL. The state would be idle unless it isn’t.

The only caveat is that it needs to hit something at boot. Because if it hits nothing, it would be an error and the REST sensor may never get created.

Think of it like a vacuum cleaner. It is:

  1. Docked
  2. Going to Target
  3. Cleaning
  4. Returning to Dock

Most of the time it is “Docked” but HA is hitting it to ask it … what are you doing? And it says “Docked”.

It is a state-based system … what state is the sensor in? The equipment doesn;t need to tell HA what it is doing, it is polled. That said, you can also send state change messages from your system to HA. Read up here:

I have a sequence of about ten minutes from start to finish which starts with “curl http://yun.local/arduino/on
After the sequence the state is “off”
During the sequence the state changes and I want it show in the lovelace card.
For instance “running” “compressor on” “compressor off” “valve open” etc.
That should make clear what I am trying to accomplish?
In the meanwhile thank you for your time!

I read the REST API Docu and think that’s what I need but it doesn’t make any sense to me; have no idea what to do since I do not understand it all.
That’s due to my lack of knowledge ofcourse…

Let’s break this down into components so that I can understand and then suggest/help.

NOTE: This is one way, I am sure there are many more. And I am not testing this, it may need a few tweaks. Also I tend to write things all directly in YAML, some of the like the automation you can build in the GUI editor.

  1. You have a sequence in which you want to run curl to http://yun.local/arduino/on
  2. You want some switch in the GUI to send that command
  3. You (have or will have) something like http://yun.local/ardino/status which would return the status
  4. It is not clear to me why you have an http://yun.local/ardiuno/off because you say it runs through a sequence and then ends (unless you want to be able to stop it at anytime)

What you need is 4 things.

  1. An input_boolean (lets call it input_boolean.ardiuno for this exercise). As it is an input_boolean it has two states, it is either “on” or “off”. No YAML here, you can just use a helper to create it.

  2. You have a shell_command that runs curl. Let’s call it shell_command.run_ardiuno. That would be very simply (you put you proper curl command here, below is just example):

shell_command:
    run_arduino: >
        curl http://yun.local/arduino/{{sequence}}

Note that you would be passing in the state of the switch here (“on” or “off” and hence I use a template to decide which URL to hit.

  1. You need a sensor that polls the state of the process. It should return JSON and could be very simple like
{"status": "running"}

of course here I am assuming you are writing this and will return your states that you wish (idle, running, emptying, filling, off)… YOu decide these but they should be states, I would say they are idle, running, emptying, filling … where idle is off. But that is your code. Think of the vacuum example never on or off, it is … docked, moving to target, cleaning, mopping, returning, emptying.

The sensor might be like this:

sensor:
  - platform: rest
    name: Arduino Status
    scan_interval: 30
    resource: http://yun.local/ardiuno/status
    value_template: {{value_json.status}}
  1. Now you just need a automation that triggers on the change of the switch. Here you can do many things but I am only going to show you the “on” part as it is unclear if you want the “off” part.
alias: Run Ardiuno
description: ""
trigger:
  - platform: state
    entity_id: input_boolean.ardiuno
    to: "on"
condition: []
action:
  - service: shell_command.run_ardiuno
    data_template:
      sequence: {{ states('input_boolean.ardiuno') }}
mode: single

So when the switch goes on, run the command to turn on the sequence. Note that is uses data_template to send “on” or “off” from the state of the switch. The state of the sensor above is monitored all the time at 30 seconds, you can change that. You may want to add update entity on the sensor at the end to get immediate status change as the last step in that automation.

You probably also need to do something as a condition in the sequence like if the status moves to “off” or whatever you call it, it sets the input_boolean back to off.

What would be left is simply some Lovelace Entities card with the sensor and the input_boolean. It would show the switch for the input_boolean so you can click it and you can display the state of the sensor.

Now this is a solution which is poll based. It polls the process and reports back. You could implement it the other way and have a simple state sensor which you push the state into Home Assistant using its own REST API. That would be best if you do not want to use Home Assistant to start/stop things and always use something else. But I am assuming you want it all in Home Assistant and that is what this type of solution would provide.

Oh wow, what an extensive piece…
I’m so sorry to react until now but I had some physical issues and a hospitalization last weeks.
Going better now.
After the procedure I had some time to read a bit about the possibilities and have decided to change the yun code to use MQTT.
I think that comes a lot closer to what I want and is not dependant of the polling stuff.
Again, many thanks for your time!!!

Jan