Do an action via the API

Hello,
I'm using HA with a luxPower inverter/battery system, I monitor and control the inverter with my own python code which interfaces with HA via its API.
It all works very well except occasionally something gets stuck and readings stop updating and I have to manually click a button (button card)


on the HA dashboard to refresh the LUX Data.
For the life of me I cannot figure out how to automate this (single click) from my python code via the API.
Sorry if this is a daft question, any help gratefully received.

Do the entities report unknown or unavailable or do the values stop updating?

Unknown or unavailable states are easy to trigger from, e.g.

trigger: state
entity_id: sensor.foo_bar
to: unknown

If the values just stop updating then you need to determine how long you want to wait to make sure they are really stuck and not just staying on the same value because that is what is happening in reality. e.g if that value is 30 seconds then you can trigger an automation like this:

trigger: state
entity_id: sensor.foo_bar
to:  # null to will trigger on any state that exists continuously for 30 sec.
for:
  seconds: 30

Thanks for responding. The entity values just stop updating, I can easily detect this with my external python code by checking the "date of last update" entity. The only problem is automating the refresh process from my external python code VIA the HA API. I guess this is really an API question.
Thanks again for your input.

Rest api supports calling services.

Yes, thanks, I'm aware of that documentation, it's not overly helpful.
I've tried calling what I believe to be the refresh service, ie:
"http://192.168.0.29:8123/api/services/luxpower/luxpower_refresh_registers"
with the bearer token header and with an empty json {} field but I get an error 400 - bad syntax, which make sense.
The documentation is not clear about what other headers are required and what if any json data is needed.
Any clarification or ideas would be most helpful.
Thanks again.

you could also use

and design your own interface

I'd remove the luxpower_ from the action name. It's a post request.

Pete, thanks for that suggestion, sadly that merely changes the error (from 400) to 404
So that looks like a step backwards.
I can't help wondering if I should be putting something in the json data.
Any other ideas?

I'm probably the last guy who should comment since I know next to nothing about templates.

What I do know is that each entity has a (hidden) last_changed & last_updated property which you could use to determine how long it's been since the last update on that sensor.

Hopefully someone with more template clout than me can guide you on how to set up a trigger-based template sensor which fires if one of those properties is greater than X amount of time. You'd then be able to set up an automation to call luxpower_refresh_registers quite easily without having to involve a direct API call.

First step is to try a different service to verify you can call it correctly. Try a switch turn_on.

You may need an empty body. What does the action call look likes in YAML if configuring it via the automaton editor?

curl \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"entity_id": "switch.christmas_lights"}' \
  http://localhost:8123/api/services/switch/turn_on

Thanks but I want to do it via the API, it must be possible and it's bugging me.

All other services (switches etc) work fine, I'm fiddling with dozens of them without problems.
The yaml for the refresh button card is this:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: luxpower.luxpower_refresh_registers
  service_data:
    dongle: BA30100099
  target: {}
entity: ""
icon_height: 50px
icon: mdi:cloud-refresh
name: Refresh LUX Data
show_state: false

Does that help, it's greek to me!

The data should be

dongle: BA30100099

And the domain is luxpower and the action is luxpower_refresh_registers

Pete, Thanks for that - it seems to work.
At least insofar as it now gives no error codes but: it's impossible to say, at the moment, if did actually do a refresh.
To find that out I'll have to wait for the next random lock-up event.
Thanks again, and also to all who have contributed, this is a good community.