Streamline multiple history_stats entries?

I’ve only recently discovered history_stats and I’ve started going bonkers on using it in conjunction with the Harmony remote integration to get stats on gaming/TV time used. The problem is, I’m ending up with loads of repetition which is starting to look a bit unmanageable. For instance, I have this at the moment for the last 24h use:

  - platform: history_stats
    name: Gaming time 24h - Xbox
    entity_id: sensor.harmony_remote_lounge
    state: 'Xbox Series X'
    type: time
    duration: 24h
    end: '{{ now() }}'
    
  - platform: history_stats
    name: Gaming time 24h - PS5
    entity_id: sensor.harmony_remote_lounge
    state: 'PS5'
    duration: 24h
    end: '{{ now() }}'
    
  - platform: history_stats
    name: Gaming time 24h - Switch (docked)
    entity_id: sensor.harmony_remote_lounge
    state: 'Switch'
    type: time
    duration: 24h
    end: '{{ now() }}'
    
  - platform: history_stats
    name: TV time 24h - Netflix
    entity_id: sensor.harmony_remote_lounge
    state: 'Netflix'
    type: time
    duration: 24h
    end: '{{ now() }}'
    
  - platform: history_stats
    name: TV time 24h - Sky
    entity_id: sensor.harmony_remote_lounge
    state: 'Sky Q'
    type: time
    duration: 24h
    end: '{{ now() }}'

… and I’d like to do the same thing for a week duration, and maybe month, which is going to result in a massive load of text which is mostly all the same lines, especially when I will probably also have a template for each one to convert the decimal hours into hours and minutes. Is there any way to reduce some of that duplication and bloat?

I was hoping I’d be able to do something like this:

  - platform: history_stats
    type: time
    duration: 24h
    end: '{{ now() }}'
    entities:

      - entity_id: sensor.harmony_remote_lounge
        name: Gaming time 24h - Xbox
        state: 'Xbox Series X'
    
      - entity_id: sensor.harmony_remote_lounge
        name: Gaming time 24h - PS5
        state: 'PS5'
    
      - entity_id: sensor.harmony_remote_lounge
        name: Gaming time 24h - Switch (docked)
        state: 'Switch'
    
      - entity_id: sensor.harmony_remote_lounge
        name: TV time 24h - Netflix
        state: 'Netflix'
    
      - entity_id: sensor.harmony_remote_lounge
        name: TV time 24h - Sky
        state: 'Sky Q'

… but I see that’s not valid. Are there any other options to do the same thing with less lines & repetition? Am I using the wrong tool for the job? Wondering if maybe I should be using the sql interface instead in some way…

You could use YAML Aliases and Anchors to streamline the duplicates. For example, instead of repeating these three lines for each entity:

    type: time
    duration: 24h
    end: '{{ now() }}'

you define it once then refer to it everywhere else it is needed.

I provided examples in this post:

That is perfect! Thanks very much, it works a treat.

It was even happy for me to include the entity_id in the anchor. so I could get away with doing this:

  - platform: history_stats
    name: Dummy Lounge 24h
    state: 'dummy'
    <<: &remote_lounge_24h 
        entity_id: sensor.harmony_remote_lounge
        type: time
        duration:
            hours: 24
        end: '{{ now() }}'

  - platform: history_stats
    name: Gaming time 24h Xbox
    state: 'Xbox Series X'
    <<: *remote_lounge_24h

  - platform: history_stats
    name: Gaming time 24h PS5
    state: 'PS5'
    <<: *remote_lounge_24h
    
  - platform: history_stats
    name: Gaming time 24h Switch (docked)
    state: 'Switch'
    <<: *remote_lounge_24h
    
  - platform: history_stats
    name: TV time 24h - Netflix
    state: 'Netflix'
    <<: *remote_lounge_24h
    
  - platform: history_stats
    name: TV time 24h - Sky
    state: 'Sky Q'
    <<: *remote_lounge_24h

… which is a massive improvement!

2 Likes