#rest commands in automations

1 messages ยท Page 1 of 1 (latest)

mellow jackal
#

I have an external sensor running a web server that provides sound measurements. How I used to have it set up in HA is that I would have a rest sensor in my configuration.yaml file. All worked well, but for some reason the scan_interval of only the first sensor was used. So if I had a sensor for 5 seconds first and then a sensor with an interval of 5 minutes it wouldn't respect the 5 minutes and still scan it every 5 seconds. Not sure why, but I wanted to move away from that to begin with.

Now I'm trying to set up a rest_command in the configuration.yaml file and a template sensor. An automation calls the command, for example rest_command.get_sound_level and it should update the sensor.

The issue I'm running into is that I can't seem to be able to call the rest command from an automation. I've tried following the docs, but it doesn't seem to completely match up with what I'm supposed to put into the automation editor (both in visual and yaml mode)
In my configuration.yaml file I have:

rest_command:
  get_sound_level:
    url: http://192.168.2.46:3000/sound

Question is basically just how do I set up the template in the configuration.yaml file and how do I make an automation use the rest command to update the sensor? I've been trying thins for hours now, but the more forum posts and docs I read, the more confused I get. Especially because even after verifying the configuration.yaml file and restarting HA, automations doesn't seem to recognize the rest command.

fervent ember
#

You can't use a rest command to update a rest sensor. That's sort of like asking Google Home to run an Alexa routine. You need a rest sensor, so it would be worth figuring out what is going wrong with that.

mellow jackal
# fervent ember You can't use a rest command to update a rest sensor. That's sort of like asking...

It wasn't a rest sensor I was trying to update, it was a template. Sorry if I'm being a bit confusing, don't have much experience with HA.

If a rest sensor should work then that would be ideal.

This is the sensor bit of my configuration.yaml file:

sensor:
  
  - platform: rest
    resource: http://192.168.2.46:3000/sound
    name: "LAB Sound Level"
    unit_of_measurement: "dB"
    value_template: "{{ value }}"
    scan_interval: 300

  - platform: rest
    resource: http://192.168.2.80/CO2
    name: "CO2 Slaapkamer Sybren"
    unit_of_measurement: "PPM"
    value_template: "{{ value }}"
    scan_interval: 30

  - platform: rest
    resource: http://192.168.2.80/TEMP
    name: "Temperatuur Slaapkamer Sybren"
    unit_of_measurement: "ยฐC"
    value_template: "{{ value }}"
    scan_interval: 30

  - platform: rest
    resource: http://192.168.2.80/PRESSURE
    name: "Luckdruk Slaapkamer Sybren"
    unit_of_measurement: "hPa"
    value_template: "{{ value }}"
    scan_interval: 30

  - platform: rest
    resource: http://192.168.2.80/HUMIDITY
    name: "Luchtvochtigheid Slaapkamer Sybren"
    unit_of_measurement: "%"
    value_template: "{{ value }}"
    scan_interval: 30

They all work, but only the scan interval of the first sensor is used. So I now have one with 5 minutes up top and all sensors will be scanned with a 5 minute interval. When I put one of the others up top it scans with a 30 second interval.

fervent ember
#

Are the values definitely different every time? If not but you want the sensor to update you need to set force_update to true.

mellow jackal
#

I could give that a try, but all these sensors change a lot over time.

fervent ember
#

You could also turn on debug for the rest integrations and see what's happening. I would guess that because you're making multiple requests at the same time that the end device is possibly not responding happily.
Does it have another endpoint that can provide all the data by any chance?

mellow jackal
#

What do you mean with another endpoint?

#

I'll remove the sensor for the 3 last sensors that are coming from the same device. See if that makes a difference.

#

Okay this is a me problem

fervent ember
#

Yeah. That might change things. I have a number of sensors and they all scan at whatever I set ๐Ÿ™‚

mellow jackal
#

With just one of the sensors being read out from the device it works fine. Seperate interval for the CO2 and sound level work fine.

#

Gonna have to figure that out then. Thx for the help either way.

fervent ember
#

Then I suspect my theory of "you're hitting the device multiple times at the same time" may be correct. Essentially you're DDOSing it.

#

Is there something like http://192.168.2.80/ALL?

mellow jackal
#

Looking at the logs from the device it seems to be handling it, but idk, maybe it is having issues that don't get logged.

mellow jackal
#

Could I turn that device into a HA device that contains those sensors in some way? That would be the most 'neat' way of doing it, right?

#

The same way my phone is a device in HA, where I can get battery capacity as if it were a sensor.

fervent ember
icy spear
#

Hello, i don't wanna hijack this topic but i think i have the same problem...
this is the response of my rest sensor

  "time": "2024-10-30 13:26:18",
  "boot_time": "1704",
  "battery_voltage": "14",
  "timeToSleep": "600000000.00",
  "temperature": "21.20",
  "humidity": "58.60"
}```

and in configuration.yaml i have this

rest:
- resource: http://192.168.1.171:61111/sensors/dormitor_tempAndHum?last=1
sensor:
- name: "Senzor dormitor"
unique_id: temp1dormitor
value_template: "${{value_json.temperature}}"
json_attributes_path: "$."
json_attributes:
- "temperature"
- "humidity"
- "battery_voltage"```

but in dashboard i see only one value, the one from template...
How can i see all 3 of them directly?

fervent ember
icy spear
icy spear
#

i found an example on internat which was also having something like

#    sensors:
#      dormitor_time:
#        value_template: "{{ state_attr('sensor.dormitor_temperature_and_humidity', 'time') }}"
#        friendly_name: "Dormitor Sensor Time"
#      dormitor_boot_time:
#        value_template: "{{ state_attr('sensor.dormitor_temperature_and_humidity', 'boot_time') }}"
#        friendly_name: "Dormitor Sensor Boot Time"
#      dormitor_battery_voltage:
#        value_template: "{{ state_attr('sensor.dormitor_temperature_and_humidity', 'battery_voltage') }}"
#        friendly_name: "Dormitor Battery Voltage"
#      dormitor_time_to_sleep:
#        value_template: "{{ state_attr('sensor.dormitor_temperature_and_humidity', 'timeToSleep') }}"
#        friendly_name: "Dormitor Time to Sleep"
#      dormitor_humidity:
#        unique_id: test_temp_hum
#        device_class: humidity
#        value_template: "{{ state_attr('sensor.dormitor_temperature_and_humidity', 'humidity') }}"
#        friendly_name: "Dormitor Humidity"```
fervent ember
#

At the bottom of that modal they should be there. Assuming the rest integration could find them

icy spear
fervent ember
#
rest:
    - resource: http://192.168.1.171:61111/sensors/dormitor_tempAndHum?last=1
      sensor:
        - name: "Senzor dormitor"
          unique_id: temp1dormitor
          value_template: "{{value_json.temperature}}"
        - name: "Senzor dormitor"
          unique_id: battery_voltage1dormitor
          value_template: "{{value_json.battery_voltage}}"
        - name: "Senzor dormitor"
          unique_id: humidity1dormitor
          value_template: "{{value_json.humidity}}"

Try this instead.

#

If you want 3 sensors, you need to specify 3 sensors ๐Ÿ™‚

icy spear
#

but...can't i store multiple attributes in a single sensor?

#

finally

#

it works

#

but idk i was expecting to see all 3 attributes linked to a single line

#

but also, fore xample the smart socket is a single sensor but i have multiple lines for each attribute...

fervent ember
#

Not the way you've done it. I'm on mobile so writing and testing YAML is kind of hard.
If you want them as attributes try removing the json_attributes_path from your version.

icy spear
#

and...is there a "right" way to do the things?

#

if i use "your" way, i'm sure that i can create scenarios based on those values, but in "my way", i still can do it?woth attributes?

fervent ember
#

It depends if you need historical data tracking and so on. You should remove the $ from your templates by the way. That's why the main sensor is showing as $21.00 ๐Ÿ˜‰

#

(Copy paste error on my part to preserve it!)

icy spear
#

in history i see only the "main" value

fervent ember
#

Yes. You don't get statistics for attributes.

icy spear
#

thank you, so i'll go with "your way"

#

in your way a single request it's made, for all values, right?

#

i saw some posts (maybe some of them too old) saying that there are 2 ways for rest sensors...one which will do a single request for all atributes, and another who will make a request per every attribute

fervent ember
#

The way I did it will only call the endpoint once and create all the sensors.

icy spear
#

thank you for your help

limber canopy
#

Hi, Rosemary, could you provide some help

#

I want to toggle a switch, gpio5 on an espeasy device. I made this in configuration

#
  - switch:
      name: X19 gpio5
      
      command_on: >
        curl -k http://192.168.4.36/control?cmd=GPIO,5,1
      command_off: >
        curl -k http://192.168.4.36/control?cmd=GPIO,5,0
      command_state: curl http://192.168.4.36/control?cmd=status,gpio,5
      value_template: >
        {{value_json.state}}
      icon: >
        {% if value_json.state == 1 %} mdi:toggle-switch
        {% else %} mdi:toggle-switch-off
        {% endif %}      ```
#

I made this card in dashboard, no other way I find

#

Now I have something like that, and the espeasy device execute commands, but HA not receive response of gpio5 state