Run Script (or Fire Event) when Opening Lovelace View

I’m looking to obtain thermostat information automatically when opening a view in Home Assistant Lovelace. The thermostat data is accessible via a script and I’ve setup the script entity with a “RUN” button so I can run the script manually from Lovelace. Now I’d like to run the script automatically when the view opens so I can see the latest thermostat information without having to click the “RUN” button.

I’ve searched wide and far with no success. Any ideas are appreciated.

Maybe by triggering on the sensor path attribute using browser mod?

1 Like

Thanks for the suggestion. I managed to get this to work after installing and configuring browser_mod.

automation:
  - alias: "Open Browser"  # get the thermostat data when home assistant "thermostats" view is opened
    mode: single
#    variables:  # can't get these to work
#      browsers:
#        - sensor.roy_mac_safari
#        - sensor.roy_ipad_safari
#        - sensor.roy_iphone_safari
#      view: "/lovelace/thermostats"
    trigger:
      - platform: state  # in home assistant; switched to "thermostats" lovelace view
        # entity_id: "{{ browsers }}"  # can't get this to work
        entity_id:
          - sensor.roy_mac_safari
          - sensor.roy_ipad_safari
          - sensor.roy_iphone_safari
        attribute: path
        to: "/lovelace/thermostats"
      - platform: state  # opened home assistant in "thermostats" lovelace view (uses condition)
        # entity_id: "{{ browsers }}"  # can't get this to work
        entity_id:
          - sensor.roy_mac_safari
          - sensor.roy_ipad_safari
          - sensor.roy_iphone_safari
        to: '1'
    condition:
    - condition: template
      value_template: "{{ trigger.to_state.attributes.path == '/lovelace/thermostats' }}"
    action:
      - alias: "Get Thermostats"
        service: script.turn_on
        target:
          entity_id:
            - script.thermostats_get

One downside is that I need to manage the browser_mod sensors for each browser.

I wasn’t able to get the variables to work which would have reduced the redundant entities.

I discovered a better (for me) way to do this using App Events. It will fire an event when the Home Assistant iOS App becomes active. (There is an Android event_type too). This doesn’t fire when opening Home Assistant via a browser.

automation:
  - alias: "HA App Became Active"  # get the thermostat data when home assistant is opened
    mode: single
    trigger:
      - platform: event
        event_type: ios.became_active
    condition:
    action:
      - alias: "Get Thermostats"
        service: script.turn_on
        target:
          entity_id:
            - script.thermostats_get
1 Like

Thanks! This works great for iOS, but what Android event type did you refer? There seems to be no android equivalent.

Did you find an equivalent?

No. There is none. You have to resort to more hacky solutions for android.

I think a simple way might be setting us a REST API in HA and then using a (hidden) image in UI that will call the API via GET method when UI is loaded.