Automation based on Lovelace view?

[Edited for clarity]

Is it possible for an automation to be triggered when a particular view in Lovelace is being displayed?

I thought it would be perfect for triggering an automation to update information only sensors on that page.


We have a new service home_assistant.update_entity to request the update of an entity, rather than waiting for the next scheduled update. This means you can set a really long scan_interval in the platform_options and then update on demand, ideal for those services where you have low API limit.`

You can make yourself an invisible card and then hand it all sensors you are using in that view.
Then you have your custom_card call the update service. It’s any async method though, so there might be some delay until they all update.

https://developers.home-assistant.io/docs/en/frontend_data.html#methods

Sorry, my fault for not being clearer. I meant could an automation be triggered when a view is selected. I’m not yet up to writing a custom component/card. I’ll edit the OP.

I doubt there is a way via automation, so here you go:

import {
  LitElement, html
} from 'https://unpkg.com/@polymer/lit-element@^0.5.2/lit-element.js?module';

class SensorUpdateCard extends LitElement  {
static get properties() {
    return {
      hass: Object,
      config: Object,
    }
  }

  _render({ hass, config }) {
    if (config.entities.length > 0){
    for (var i = 0; i < config.entities.length; i++){
      try{
        this._update(config.entities[i]);
      } catch (error){
        console.log("Failed to update Entity at Positon "+i+" "+config.entities[i]);
        console.log("Error "+error);
      }
    }

     console.log("Updated "+config.entities.length+" Entities!");
     config.entities = [];
    }
    return html``;
  }

  setConfig(config) {
    if (!config.entities) {
      throw new Error('You need to define entities');
    }
    this.config = config;
  }

  getCardSize() {
    return 0;
  }

  _update(entity_id) {
    this.hass.callService('homeassistant', 'update_entity', {
      entity_id: entity_id
    });
  }
}

customElements.define('sensor-update-card', SensorUpdateCard);
resources:
  - url: /local/sensor-update-card.js
    type: module
  ...
   cards:
      - id: 10b9a5f104ec44238c4240dd1cc035fc  # Automatically created id
        type: custom:sensor-update-card
        entities:
          - sensor.sensor1
          - sensor.sensor2

Blimey! Thanks!!

I’ll give that a go once I’ve upgraded to 0.81

Made it so it only triggers once you load the view.
At first I was confused, since the update service didn’t work.
Only then I found out that I needed to update ha :stuck_out_tongue:

@perdemot
Hi,
I am trying to use the code you provided without any luck so far. I end up having:

Custom element doesn't exist: sensor-update-card.

in lovelace. I am running on hass.io with the latest version.

Apart for putting the js code in the sensor-update-card.js, adding that file to the resources, and adding the sensor-update-card in the view as shown is there something else I am missing. Is your code still working in the current ha version?

Thanks,

GV

Hi @greengolfer,

do you still need something like this?
Or did you find a work around for your problem?
Otherwise I will check if it still works/update it.

No. I changed my mind :slight_smile:
GV