Homekit Infused 5 (HKI) v2023.1.3

no, this is not the case. I was experiencing issues with all of these template running inside the button card configs. frontend having to calculate all of that display continously is a serious task. Though, I suspect if one doesn’t have that many entities, the system might fare well enough.

In this case, using these backend sensors, the buttons only have to show the state, which is much easier… I also think is is of a better architecture tbh, have the backend and the frontend do what they are designed for, makes for a better system performance and reliability.

talking about templates, you might want to check these for the existence of an ‘else’ clause, which is really necessary to catch unforeseen situations/states. Also you could start using the states() notation:
in stead of

{{ states.binary_sensor.motion_toilet.attributes.temperature }}

use:

{{ state_attr('binary_sensor.motion_toilet','temperature') }}

which is also safer as explained in the docs regarding state not having a value yet, and the way that is handled by the system.

Well I didn’t know this either :rofl::joy:. I did see those docs but only very briefly. I will change my sensors and see what happens :sweat_smile::+1:. Thanks for taking the time to help out an amateur xd

When you use this template as sensor does it updates every time you switch on /off the lights? because I had it this way and wasn’t updating!!

{{ states.light | selectattr("state", "equalto", "on") | list | length }}

this is what I had to calculate the lights on, but for some reason wasn’t updating the state!
Thanks

Have you tried using the

- type: custom:state-switch
        entity: hash

its very useful to save space and make sub-menu that displays in the same space, specially if you have alot of thing to display in the same area. I use it to diplay devices/ lights, etc withen the floor

I think he is referring to the templates used in a template sensor, not in lovelace itself.

But could you give an example of your state switch? Or a screenshot, I am interested though.

its a template sensor !!

and here an example an the code

          - type: custom:decluttering-card
            template: button
            variables:
              - entity: input_boolean.dummy_nav
              - name: Planta Baja
              - icon: mdi:home-floor-0
              - show_state: false
              - opacity: 0.8
              - tap_action:
                  action: navigate
                  navigation_path: "#planta_baja"
                  haptic: light
              - hold_action:
                  action: navigate
                  navigation_path: "#planta_baja"
                  haptic: light
  - type: vertical-stack
    cards:
      - type: custom:state-switch
        entity: hash
        default: dormitorio_principal
        states:
  # planta_baja 
          planta_baja:
            type: vertical-stack
            cards:
              - type: custom:decluttering-card
                template: header
                variables:
                  - content: '##### Planta Baja'
              - type: horizontal-stack
                cards:
                  - type: custom:decluttering-card
                    template: button
                    variables:
                      - entity: group.planta_baja
                      - name: Planta Baja
                      - icon: mdi:spotlight


Also another suggestion that I have and working so great, instead of using name or friendly names i added all entities to secrets so i can change the names there without having to change them everywhere and i point them when ever i want to use them as !secret name, here is an example of my secrets temps and where i use them

# ClimaTE A/C

ac_aire_1: Aire 1
ac_aire_2: Aire 2
ac_aire_3: Aire 3
ac_aire_4: Aire 4
ac_aire_5: Aire 5
ac_aire_6: Aire 6

 # Swicthes Names temps
# Planta baja
switch_pb2_1: graderia izquierda pb2_1
switch_pb2_2: graderia izquierda pb2_2
switch_pb2_3: graderia izquierda pb2_3

and where ever you want to use them

       light.dorm_p_4:
         friendly_name: !secret light_dorm_p_4
         icon: mdi:bulb

I think this way your coding can be used as general specially for entities.

1 Like

Ah ok, I understood you wrong. However the secrets file to have friendly names? I like the solution, but actually there is already a file specifically for that (customize.yaml). I use that and define most of the names and icons in there.

Though I did think of using the secrets file to put in entities. The problem is that I use categories for lights (e.g. bedroom). I can make it easier to set up if I take out categories and have buttons arrange like Apple’s Homekit does. Though I love the ideas. I will play around with it. Thanks

o yes, you’re right, it doesnt…
well can always use this then:

- alias: 'State changed Light'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in state_attr('group.all_lights_only','entity_id') }}
  action:
    service: homeassistant.update_entity
    entity_id: sensor.lights_on

since I do all my counting in Python, I forgot about that, sorry…

just to be complete, this would probably do also:

{{ expand('group.all_lights_only')|selectattr('state','eq','on')|map(attribute='name')|list|count}}

So your last line I should try it as a template sensor right?

- platform: template
  sensors:
    counter:
      your_code_here

Srry I am fairly new with using templates (as you might have seen in my code :rofl::joy:)

I’m just a bartender with HA obsession (hobby).

yep:

sensor:
  - platform: template
    sensors:
      lights_on:
        friendly_name: Lights on
        value_template: >
          {{ expand('group.all_lights_only')|selectattr('state','eq','on')|map(attribute='name')|list|count}}

but, do note this doesnt auto update unfortunately… sorry I forgot about that. Still, adding this small automation does the trick, and makes it much easier than coding all the individual entities.

dont be sorry! be happy, you’re about to embark on a journey about one of the most powerful aspects of HA!
All the frontend niceties in Lovelace are nowhere without great backend templates :wink:

Thanks I’ll try it

You are correct, well it’s just a lot of work haha. Been working on HA for months now and I think my setup looks decent on the frontend. It’s just that I want optimizations on the backend. I’d love backend material to be like my button template. I use a single template for every button (even the ones with the notification badge).

Thanks a lot though!

just to make a correction so the automation works:

- alias: 'State changed Light'
  trigger:
    platform: event
    event_type: state_changed
    event_data:
      entity_id: group.alllights 
  action:
    service: homeassistant.update_entity
    entity_id: sensor.lights_on

If not the automation won’t trigger.
and the sensor would be:

- platform: template
  sensors:
    lights_on:
      friendly_name: Lights
      unit_of_measurement: 'On'
      value_template: >
        {{ expand('group.alllights')|selectattr('state','eq','on')|map(attribute='name')|list|count}}

So it will check only lights inside the alllights group(which is default light group created by HA)
thanks

what’s the correction? I’ve made my own group.all_lights_only group because in the group.all_lights (not group.alights) group (which is soon to be deprecated…) also light groups are included.

btw, the whole idea behind my suggestion to @jimz011 was because he mentioned including and excluding specific entities for his sensors.

Adding only the desired entities to a group, is what this is all about, and makes it so flexible. Of course we can also use the default all_* groups (for now)

Was the automation trigger without the event_data? because i tried to make it trigger without success, so I added the event_data and took off the condition and worked fine. I’ve just used the Ha group , but i believe it should work for any group.

thanks a lot for the suggestion.

1 Like

this wont work, because the trigger now is set to the group, and that wont change state when one of its entities changes, unless they go all off or on, or change from that state.

thats why the whole effort is done to use the condition, in which you set when the state change trigger is actually passed (evaluated to true) and then fires the action.

  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in state_attr('group.all_lights_only','entity_id') }}

is therefor necessary, if you want to check for items of that group, sing the state_changed trigger

if you want a bit of fun, and have more groups you want to monitor…:

- alias: 'State changed (all)'
  id: 'State changed (all)'
  initial_state: 'off'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: template
    value_template: >
      {% for x in
           ['family',
            'hubs_binary_pinged',
            'critical_devices_state',
            'media_player_media',
            'device_tracker_media',
            'all_lights_only',
            'iungo_switch_switches_template',
            'iungo_switch_appliances_template',
            'binary_sensors_active_template']
        if trigger.event.data.entity_id in state_attr('group.'+x,'entity_id') %} true
      {% else %} false
      {% endfor %}
  action:
    - xxxxx

as said, I run my counters using python scripts, and the above automation calls a python script to do so :wink:

I’ve tried the automation I changed and tested when turning lights on/off and the counter changes counting how many lights on!. and I agree with mine it will count only lights in this case( since my group is only lights), but also can use en event_data a series of entities too if needed to make them all as one set of ON devices.
could be different the automation when you are calling python script? don’t know the true, but it’s good to know.
it’s just what I’m using and worked for me, please if you can add the sensor and automation i have and check if they works too, just to make sure i works
Thank you for clarifying. :slight_smile:

Hi @jimz011,

First i must say that i’m never had any experience with code at all.
i love your view, and i’m learning slowly,

i watched you last video, and i need your help with the following :
in the video at 1:59 seconds you are demonstrating the light group, when you press it shows the lights that currently on. when you click on it, i can see that all background is transparent and only the glance card shown in gray color that matched your entire theme, i tired look at your examples ant got lost totally :frowning: , my bad i’m total noob.

this is screen shot from my setup using my phone:


when i click on it , i get the following view:

i want to match the background for the card as you, and also the transparent look as you did below

this is the code i’m using for the po pup card:

  popuptest1:
    card: 
      type: 'custom:button-card'  
      color: auto
      size: 30%
      icon: '[[icon]]'
      entity: '[[entity]]'
      show_last_changed: true
      aspect_ratio: 1/1
      show_state: true
      name: '[[name]]'
      show_label: true
      tap_action:
        action: call-service
        service: browser_mod.popup
        service_data:
          title: Popup example
          card:
            type: 'custom:button-card'
            entity: light.corner_living_room_lights
          deviceID:
            - this
            - dashboard
      hold_action:
        action: more-info
        haptic: success
      styles:
        card:
          - padding-left: 10px
          # - background-color: 'rgba(255,255,240,0.9)'
          # - background-color: '#fff5bc'
          - background-color: 'rgb(100,100,100)'
          - border-radius: 15px
        name:
          - justify-self: start
          - font-weight: bold
          - font-size: 13px
        label:
          - font-size: 11px
          - font-family: Helvetica
          - padding: 0px 10px
          - justify-self: start
        state:
          - font-size: 11px
          - font-family: Helvetica
          - padding: 1px 10px
          - justify-self: start
          - text-transform: capitalize
          - font-weight: bold
        grid:
          - grid-template-areas: '"i" "n" "s" "l"'
          - grid-template-columns: 1fr
          - grid-template-rows: 1fr min-content min-content
        img_cell:
          - justify-content: start
          - align-items: start
      state:
        - value: 'on'
          styles:
            card:
              - box-shadow: 0px 0px 3px 2px var
            name:
              - color: AliceBlue
            state:
              - color: AliceBlue
            label:
              - color: AliceBlue
          id: on-icon
        - value: 'off'
          styles:
            card:
              - box-shadow: 0px 0px 3px 2px var
              # - opacity: 0.3
            label:
              - color: AliceBlue
            icon:
              - color: 'AliceBlue'
            name:
              - color: AliceBlue
            state:
              - color: 'AliceBlue'
          id: off-icon
        - value: unavailable
          styles:
            card:
              - opacity: 0.2
              - color: grey
              - '--paper-item-icon-color': grey
            label:
              - color: 'rgba(0, 0, 0, 0.0)'

can you please help me with the code?

Thank you !

First of all I don’t use browser-mod (though the results are the same). What you need to do is put a style into your popup card.

        service_data:
          title: Popup example
          style:
            background: var(--background-image)
          card:
            type: 'custom:button-card'
            entity: light.corner_living_room_lights
          deviceID:
            - this
            - dashboard
1 Like