luixal / lovelace-media-source-image-card

A Lovelace custom card for showing images stored in Media Source
GNU General Public License v3.0
21 stars 0 forks source link

freature request: add service for image update #9

Closed 532910 closed 7 months ago

532910 commented 7 months ago

I'd like to have a service to call from automation to update media-source-image-card:

type: custom:media-source-image-card
image: "media-source://media_source/local/gallery/{{ '%02d' % range(0, 20) | random  }}.jpeg"
luixal commented 7 months ago

Hi,

I don't know of any way to use a sensor to update a card. You should create a helper that keeps the value and gets updated using a service call. Then, use that helper to show the image in the card.

You can follow this steps:

  1. Create an input_text helper. i.e: input_text.random_image.
  2. Make an automation/script that updates the value of the helper with that expressión you have there: '%02d' % range(0, 20) | random
  3. Configure the card like this:
type: custom:media-source-image-card
image: |
  media-source://media_source/local/{{ states('input_text.random_image') }}
entity_id: input_text.random_image (or other entity if you want to toggle state or anything)
532910 commented 7 months ago

Yep, I know, and it works for me:

type: custom:media-source-image-card
image: media-source://{{ states("sensor.photo") }}

sensor.photo template:

media_source/local/x/{{ '%02d' % range(1, 20) | random }}.jpeg

automation:

trigger:
  - platform: time_pattern
    seconds: /10
action:
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.photo

But this solution requires helper to work. I would be nice just to call service from an automation directly:

type: custom:media-source-image-card
id: photo
image: media-source://media_source/local/x/{{ '%02d' % range(1, 20) | random }}.jpeg
trigger:
  - platform: time_pattern
    seconds: /10
action:
  - service: media-source-image-card.update
    target:
      id: photo

I'd rather not create a helper for some internal stuff.

luixal commented 7 months ago

Believe me, I HATE to have extra helpers too... but I think only integrations can have services, not custom cards.

Anyhow, a simple forced refresh would work for this, as the template would be re-evaluated. That way, the automation won't be needed either.

Let me give a try and I can include It.

luixal commented 7 months ago

I just published a new version (0.3.0) adding this feature and an example in the readme.

Can you give it a try to see if it works in your scenario and give me feedback?

Thanks!

532910 commented 7 months ago

Wow, it works like a charm!