question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trigger refresh based on time

See original GitHub issue

Feel free to dismiss this if you feel it’s out of scope for this custom card.

Is your feature request related to a problem? Please describe.

I have a button card for a switch entity where I want to show how long the switch has been on in the state_display. Showing the time was not hard, but getting it to refresh itself regularly isn’t as easy. This is what I have at the moment:

  - type: 'custom:button-card'
    entity: switch.biltak_motorvarmare
    name: Motorvärmare
    icon: 'mdi:car'
    show_state: true
    state_display: |
      [[[
        if (entity.state === 'on') {
          function humanReadableDuration(duration) {
              var hours = Math.floor(duration / 1000 / 60 / 60);
              var minutes = Math.floor((duration / 1000 / 60 / 60 - hours) * 60);

              var result = '';
              if (hours > 0) {
                  result += hours + ' h ';
              }
              
              return result + minutes + ' min';
          }

          var timeDiff = new Date().getTime() - new Date(entity.last_changed).getTime();
          return 'Stäng av (på i ' + humanReadableDuration(Math.abs(timeDiff)) + ')';
        }
        else
          return 'Lägg på';
      ]]]

Describe the solution you’d like

Native support for refreshing a card regularly. I would like to e.g. trigger a refresh every 10th second.

I’m intentionally not getting into suggestion how it should be configured here.

Describe alternatives you’ve considered

It should be possible to create some kind of time entity myself, and add it to triggers_update, but for example the built-in Time & Date sensor does not support entities that updates more often than once a minute.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
RomRidercommented, Feb 21, 2021

That should work (it’s a bit hacky, but who cares 😊 ):

  - type: 'custom:button-card'
    entity: switch.biltak_motorvarmare
    name: Motorvärmare
    icon: 'mdi:car'
    show_state: true
    state_display: |
      [[[
        if (entity.state === 'on') {
          if (this._myTimer === undefined) {
            this._myTimer = window.setInterval(() => { this.update() }, 1000 * 10) // That value 10*1000 is 10000 ms = 10 sec
          }
          function humanReadableDuration(duration) {
              var hours = Math.floor(duration / 1000 / 60 / 60);
              var minutes = Math.floor((duration / 1000 / 60 / 60 - hours) * 60);

              var result = '';
              if (hours > 0) {
                  result += hours + ' h ';
              }
              
              return result + minutes + ' min';
          }

          var timeDiff = new Date().getTime() - new Date(entity.last_changed).getTime();
          return 'Stäng av (på i ' + humanReadableDuration(Math.abs(timeDiff)) + ')';
        }
        else {
          if (this._myTimer !== undefined) {
            window.clearInterval(this._myTimer);
            delete this._myTimer;
          }
          return 'Lägg på';

        }
      ]]]

Edit: some code update

1reaction
slovdahlcommented, Feb 21, 2021

Works very well, awesome. I’ll live with this until or if the native support is ever added 👍🙇

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to automatically reload a web page at a certain time?
Note that this code will refresh based on the client local time. If you want it to be at a specific time ......
Read more >
Pages / Triggering Data View Refresh - Code On Time
Data views are only automatically refreshed by the client library when data is inserted, updated, or deleted. The user can force a refresh...
Read more >
How to Automatic Refresh a web page in fixed time
Approach 1: One can auto refresh the webpage using the meta tag within the head element of your HTML using the http-equiv property....
Read more >
Automatic page refresh in Power BI Desktop - Microsoft Learn
This refresh type allows you to update all visuals in a report page based on a constant interval such as one second or...
Read more >
PowerBI RS: Setting Data Refresh in Trigger Mode
DirectQuery enables users to continuously obtain the latest data every time they use a report, but the disadvantage is that when the report...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found