Is there a way to fire a REST query from a dashboard card (button click), and display the response?

Hello everyone,

My use case:
I am using https://distancematrix.ai/ to get an ETA home/work for my wife, with transport mode = public transports. Works great, and very useful.

How I am doing it:
Usin Restful sensors, I am able to query the information I need, format it into minutes, and use it back on the dashboard to display a quick glance value.

sensor:
  - platform: rest
    name: "Wifey ETA to work"
    resource_template: https://api.distancematrix.ai/maps/api/distancematrix/json?key=API_KEY&mode=transit&destinations=WORK_ADDRESS&origins={{ (states('sensor.wifey_phone_geocoded_location') | urlencode) }}
    value_template: "{{ ((value_json.rows[0].elements[0].duration.value | int(0)) / 60) | int(0) }}"
    scan_interval: 900
  - platform: rest
    name: "Wifey ETA to home"
    resource_template: https://api.distancematrix.ai/maps/api/distancematrix/json?key=API_KEY&mode=transit&destinations=HOME_ADDRESS&origins={{ states('sensor.wifey_phone_geocoded_location') | urlencode }}
    value_template: "{{ ((value_json.rows[0].elements[0].duration.value | int(0)) / 60) | int(0) }}"
    scan_interval: 900

What is wrong:
The API I am using is limited to 1000 calls a month, and even with scan_interval specified, the sensors are polling too much. And in fact I don’t need the information to be there all the time.

What I would like to achieve:
I could have the information requested from the API once a day in the morning, but when I do need an updated ETA, I want to be able to click the chip and make it refresh the ETA value with an on-demand query.

Fiddled with automation, template sensors, etc but eveytime I am either lacking trigger / action / possibility to return a value / …
How can I do it in your opinion? (open to redesigning the whole thing of course… I think that what I am doing today is a bit dirty anyways).

I use this service :

You can then stick it in a script and just the call the script on click. Obviously remove the scan interval (or set it to 24 hours) and have an automation to do the same call at 7am (or whenever in the morning)

Thank you for your help.

I never wrote a script, and looking at the documentation, I don’t know if it is possible to format the response.
Could you share a code snippet of how you do it?

The service I showed is simply an action. It will refresh your rest entities. By script I mean in Home Assistant - it’s next to automations in the GUI. There’s nothing complex, but here’s the yaml of one :

sequence:
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - todo.activities

Ooooooh! PERFECT!

Thank you so much! Now I can set the scan_interval to a very big value, and just use the chip click whenever I want.

You made my day!

1 Like

Glad I could assist - have a great day :slight_smile:

Thanks, and good day to you too :slight_smile: