There are some integrations I’m only interested in updating while I’m actually accessing hass from the web or the mobile app. Is this something I can do within the hass automation framework or is it only possible via a python script, appdaemon, custom integration, etc.?
I was thinking something along the lines of:
automations.yaml
- id: <automation_id>
alias: Update entity when a user is connected to the front end
trigger:
- platform: numeric_state
entity_id: sensor.websocket_api
above: 0
- platform: time_pattern
minutes: 1
condition:
platform: template
value_template: '{{ states('sensor.websocket_api') > 0 }}'
action:
service: homeassistant.update_entity
data:
entity_id: sensor.pi_hole_ads_blocked_today
entity_id: sensor.allergy_index_forecasted_average
entity_id: sensor.u_s_air_pollution_level
Then, I would have to configure the above sensors with a really long scan interval:
Has anyone tried something similar? Or is this even a good idea? I’m mostly interested in doing this to reduce hass server load but this might be more useful for rate limited platforms/apis like google ones that might cost you money if you exceed their quotas or to limit data usage if you’ve got a limited mobile data plan.
All I can say is that I do something very similar. i.e. set up the sensor with very long intervals and then call an update at programmatically controlled intervals. And it works well for me.
I wish scan_interval (or more accurately, update_frequency) was a “first class” configuration variable like friendly_name, entity_picture, icon, etc. with semantics like automations. Then you could customize how frequently an integration updates based on time (e.g., time interval/pattern, from/to times, etc.), quota (e.g., number of updates per hour, day, month; network bandwidth, storage amount; etc.), state (e.g., if at home, don’t update google travel times, update only while there is a user is accessing the front end), etc. You could also change the update frequency programmatically, like decrease scan interval when cpu utilization is above some threshold. Maybe this is more of a feature request then a configuration question.
I planned to use the same trick to decrease the scan interval when someone is connected.
However, I get some trouble with mobile app.
The number of connected client is wrong.
The probleme is:
Nobody is connected => sensor.websocket_api = 0
I open the mobile app => sensor.websocket_api = 1
I press the back button from my Android phone => The app goes in background but sensor.websocket_api stay to 1. We can think that since app is in background, connection is still alived but,
I open again the mobile app by pressing on the icon => sensor.websocket_api = 2
You can do that several time and the number of connected client will increase every time.
After rebooting the phone, the number of connected client is still wrong.
If I kill the app instead of pressing the back button. The number of client is decremented.
The issue comes from the app that go in background.
And there is no timeout, the number of connected client stay wrong forever.