#Button-card manual refresh

1 messages · Page 1 of 1 (latest)

supple ocean
#

Is it possible to manually refresh the content of a single button-card, without having to refresh the entire browser window?

Background: I have a variable that contains randomly generated text. I want to trigger an update of this card by help of the tap_action to display different random text.

I am currently using the following code to refresh the entire browser:

tap_action:
  action: fire-dom-event
  browser_mod:
    service: browser_mod.refresh

Thanks!

#

Button_card manual refresh

#

Button-card manual refresh

torn garnet
#

triggers_update might be of some help

#
type: custom:button-card
name: |
  {{ var.randomtext }}
tap_action:
  action: call-service
  service: script.generatenewrandomtext
triggers_update:
  - var.randomtext

... or something like that

supple ocean
# torn garnet ``` type: custom:button-card name: | {{ var.randomtext }} tap_action: action...

Thanks, yes, I was thinking along the same lines, i.e., calling up a script. However, I really do not know how a script can change / update a variable that is defined in my setup as follows:

  variables:
    my_variable: |
      [[[
        const number = Math.floor(Math.random() * 14) + 1; 
        const dailySpecial = {
          '1': {
            recipe: "Some text for case 1", 
            subline: "(Some subline for case 1)"
          },
...
          '14': {
            recipe: "Some text for case 14", 
            subline: "(Some subline for case 14)"
          }
        };
        var recipe = dailySpecial[number].recipe;
        var subline = dailySpecial[number].subline;
        if (number in dailySpecial) 
          return `<div>${recipe}</div><p> ${subline} </p>`;
        else 
          return "";
      ]]]
torn garnet
#

the tiggers_update should fire if the variable changes

#

oh. wait I see what you mean

supple ocean
torn garnet
#

might be worth looking at how you are defining my_variable? maybe the random integration?

#

using random with a template seems like it could work

supple ocean
torn garnet
#

maybe? hang on I'm throwing together something for you, hopefully

torn garnet
#

ok. I think I have something here

torn garnet
#

I just had it working for a second and it dumped. hang on there

supple ocean
torn garnet
#

ok pretty sure I got it here. gonna alter some things for readability and paste in a sec

torn garnet
#
template:
  - trigger:
      - trigger: event
        event_type: randomize_dailyspecial
    sensor:
      - name: "Random Special"
        state: >
          {% set attrs = this.attributes %}
          {% set keys = attrs.keys() | select("match", "^option") | list %}
          {% set choice = keys | random %}
          {{ attrs[choice] }}
        attributes:
          option1: "<div>Recipe1</div><p> Subline1 </p>"
          ...
          option[n]: "<div>Recipe[n]</div><p> Subline[n] </p>"

script:
  random_daily_special:
    alias: Randomize the Daily Special
    sequence:
      - event: randomize_dailyspecial
#

sorry took so long; tryingto "work", eat, this, and people keep calling me on the phone lol

#

then, for your custom button

#

just call the script

#

looks like youre doing a random dinner or item for dinner thing.

#

you can tap_action -> call the script

#

then triggers_update: sensor.random_daily_special

supple ocean
#

Thanks for your kind help. Will try your code asap. This is a fun card, showing the daily special from the Bobs Burgers series 😉

supple ocean
# torn garnet looks like youre doing a random dinner or item for dinner thing.

I tried your code but somehow it does not work for me. This is what I have in my package:

template:
  - trigger:
      - trigger: event
        event_type: randomize_dailyspecial
    sensor:
      - name: "Random Daily Special"
        unique_id: random_daily_special
        state: >
          {% set attrs = this.attributes %}
          {% set keys = attrs.keys() | select("match", "^option") | list %}
          {% set choice = keys | random %}
          {{ attrs[choice] }}
        attributes:
          option1: "<div>Totally Radish Burger</div><p>(Comes with Radish)</p>"
          option2: "<div>Last of the Mo-Jicama</div><p>(Comes with Jicama)</p>"
          option3: "<div>THE CHILD MOLESTER</div><p>(Comes with Candy)</p>"
script:
  random_daily_special:
    alias: Randomize the Daily Special
    sequence:
      - event: randomize_dailyspecial

After a fresh restart of HA, the template sensor "sensor.random_daily_special" is present with a state "unknown". After I manually trigger the script, the sensor becomes unavailable. What am I missing here? Any idea?

torn garnet
#

Hmm ... I feel like I ran into that somewhere else, hangon

torn garnet
#

found it.

        state: >
          {% set attrs = this.attributes %}
          {% set keys = attrs.keys() | select("match", "^option") | list %}
          {% if keys | length > 0 %}
            {% set choice = keys | random %}
            {{ attrs[choice] }}
          {% else %}
            "None"
          {% endif %}
#

on first reset, you'll need to hit the script one time to get it from 'unknown' to 'None', then subsequent presses will randomize

#

you can also change it up so that you don't get the same option on script run:

        state: >
          {% set attrs = this.attributes %}
          {% set keys = attrs.keys() | select("match", "^option") | list %}
          {% set current = this.state %}

          {% if keys | length > 0 %}
            {# Build candidate list excluding current state #}
            {% set candidates = keys | reject('equalto', current) | list %}
            
            {% if candidates | length > 0 %}
              {% set choice = candidates | random %}
            {% else %}
              {% set choice = keys | random %}
            {% endif %}
            
            {{ attrs[choice] }}
          {% else %}
            "None"
          {% endif %}
#

though it doesnt always seem to work, I've only tested it with like 4 options

#

might work better with more'

supple ocean
torn garnet
#

👍

supple ocean
# torn garnet 👍

Here is a quick screen capture showing how the card works now. As mentioned, this is just a fun card that is part of a larger dashboard project.

torn garnet
#

nice. I'm glad it worked out for you. 🙂

fickle igloo
#

FYI: In Version 5.0 of Button Card you will be able to update based on timer. dev version available for testing.

supple ocean
modern flax
supple ocean