Dynamic History Graph Configuration in Home Assistant

I recently stumbled upon a fantastic way to dynamically adjust the number of hours displayed on a history graph in Home Assistant. This feature allows users to select how many hours of data they want to view, making it incredibly flexible for different use cases. Here’s how I implemented it:

First, I created an input select entity to let users choose the number of hours. The options I included are 24, 12, 4, 2, 48, and 72 hours, with 24 hours as the default. This was straightforward using the input_select component in my configuration.yaml file.

Next, I utilized the config-template-card to dynamically update the history graph based on the selected option. By passing the selected value from the input_select entity into the history-graph card, I achieved the desired dynamic behavior. The card now adjusts the hours_to_show parameter automatically when the user changes their selection.

Here’s a quick look at the card configuration I used:

yaml

  • type: vertical-stack
    cards:
    • type: entities
      entities:
      • entity: input_select.hours_to_show
        name: Select Hours to Show
    • type: ‘custom:config-template-card’
      variables:
      • ‘states[’‘input_select.hours_to_show’‘].state’
        entities:
      • input_select.hours_to_show
        card:
        type: history-graph
        entities:
        • entity: sensor.watts
          name: Watts
          hours_to_show: ‘${vars[0]}’
          refresh_interval: 0

This setup works seamlessly, and the visual results are impressive. Users can now easily toggle between viewing recent data or a broader timeframe without needing to manually adjust the graph settings each time.

I’m excited to explore further enhancements, such as dynamically selecting different sensors or even adjusting the time range in calendar days. This feature not only improves usability but also adds a layer of personalization that makes Home Assistant even more powerful.

If anyone has questions or suggestions on how to expand this functionality, I’d love to hear them! Let’s continue to push the boundaries of what’s possible with Home Assistant.