#Template-sensor

1 messages · Page 1 of 1 (latest)

rocky swan
#

Thank you so much for your help, I’m amateur to this so plz be patient with me. I would need to do this for 6 relays. Which method would be easier?

wooden parcel
#

The if method is problably simpler, once you get confident you can go the other route to make it easier to add remove relays.

#

Opps, I forgot you will need to use a Namespace I will just update it a bit for you.

wooden parcel
#

Actually I was right first time you do not need a namespace. I think it is only when you are working with tables. Shout the original code does not work for you.

tame seal
#

you need a namespace when working with for loops

#

but with if statements you can use normal variables

wooden parcel
#

Thanks TheFes, I have been coding Lua all morning and that has all sorts of layers, you can end up with the "same" variable with different values in different layers.

rocky swan
#

I going to try this, I’ll confirm. Thanks.

#

In devices, these relays were changed from switches to light entities so they would be recognized by lights off automations. I’m currently getting an error. Where am I pasting entity id?

tame seal
#

an error is too unclear, which arror do you get?

rocky swan
#

UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'light'

tame seal
#

when do you get that?

rocky swan
#

I’m checking this in dev tools>template

tame seal
#

what is your current template?

rocky swan
#
template:
 - sensor:
     - name: "Total Lights Count Template"
       state: "{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"
white badgerBOT
#

To format your text as code, enter three backticks on the first line, press Enter for a new line, paste your code, press Enter again for another new line, and lastly three more backticks.
```yaml
example: here
```
Don't forget you can edit your post rather than repeatedly posting the same thing.

tame seal
#

please format code as code

#

anyway, works fine here

rocky swan
#

Yes this counts my lights at the moment but those on relays as 1

#

Sorry abt the format

tame seal
#

so, when do you get the error?

rocky swan
#

After pasting the new one in

wooden parcel
#

If you are pasting my code you need to change to using multiline format for the yaml at least.

#
template:
   state: >-
      {{code here}}
#

try the code on it's own with out the sensor set up. get that working first. Remember to change your sensor to the correct name for your relay.

tame seal
#

I obviously need to see the code which causes the error to help you debug it

#

No need to debug working code

#

you are saying you have code in devtools > template which gives this error

#

what is that code

rocky swan
#
template:
   state: >-
      {%- set count = states.light | rejectattr('attributes.light.switchman_kitchen_left_light', 'defined') | selectattr('state', 'eq', 'on') | list | count %}
{%- if states('sensor.relay') == 'on' %}
{%- set count = count + 4 %}
{%- endif %}
{{count}}

Should it look like this?

#

UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'light'

tame seal
#

no, this part is wrong rejectattr('attributes.light.switchman_kitchen_left_light', 'defined')

#

you should have kept it as is, that part was in place to reject light groups

rocky swan
#

Oh I see

#

Why would we reject light groups?

#

Current code does not count light groups

tame seal
#

because if you don't reject them, it will count the individual lights and the light group itself

rocky swan
#

At the moment my code doesn’t count light groups. If there are 2 groups with 4 lights each, it shows 8

#
template:
   state: >-
      {%- set count = states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count %}
{%- if states('sensor.relay') == 'on' %}
{%- set count = count + 4 %}
{%- endif %}
{{count}}

Please confirm this

tame seal
#

yes, because it had this: rejectattr('attributes.entity_id', 'defined')

tame seal
#

but you changed that to rejectattr('attributes.light.switchman_kitchen_left_light', 'defined')

#

which broke it

rocky swan
#

So use the code u gave without adding anything?

tame seal
#

and what is the state of sensor.relay?

rocky swan
#

template:
state: >-
0

#

What I’m not getting at the moment is how r we specifying the number of lights to a particular relay

#

There are 6 relays, some have 2, some 4 lights

#

Where should I add the id’s for these relays?

tame seal
#

easiest is to do a separate if for each relay

#

replace sensor.relay with the actual entity id (I would assume it's actually a switch)

rocky swan
#

Ok

#

Great that’s what I was uncertain abt

tame seal
#
{%- set count = states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count %}
{%- if is_state('sensor.relay1', 'on') %}
{%- set count = count + 4 %}
{%- endif %}
{%- if is_state('sensor.relay2', 'on') %}
{%- set count = count + 2 %}
{%- endif %}
{{count}}
#

like that, but then all 4, and the correct entity_id's and corresponding number of lights

rocky swan
#

I’ll confirm. Thank u so much

tame seal
#

You could also do:

{%- set count = states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count %}
{%- set relays = 
  {
    'switch.relay_1': 4,
    'switch.relay_2': 2,
    'swich.relay_3': 4,
    'switch.relay_4': 2
  }
%}
{% set count_relay = relays.items() | selectattr('0', 'is_state', 'on') | map(attribute='1') | sum %}
{{ count + count_relay }}
rocky swan
#

I’m getting this error with both codes, probably my formatting.

#

Think I got it

#

Unfortunately, it still counts the light as 1

tame seal
#

What's your current code

rocky swan
tame seal
#

Please paste it anyway

rocky swan
#

Having trouble with the format. Ok

#
template:
   state: >-
      {%- set count = states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count %}
{%- set relays = 
  {
    'switch.switchman_kitchen_left_light': 4,
    'switch.switchman_kitchen_middle_light': 2,
  }
%}
{% set count_relay = relays.items() | selectattr('0', 'is_state', 'on') | map(attribute='1') | sum %}
{{ count + count_relay }}
tame seal
#

So you say that if you toggle those switches, the output changes with 1 and not with 4 or 2

rocky swan
#

no the formatting is wrong, this one gives an error

#

missed comma between flow collection entries (29:2)

26 | template:
27 | state: >-
28 | {%- set count = states.lig ...
29 | {%- set relays =
-------^
30 | {

tame seal
#

You need to indent the entire template

#

Not just the first line

rocky swan
#

lol got it sorry

#

restarting...

wooden parcel
#

You do not need to restart for templates, just go to Dev Tools>YAML scroll down the list to templates and click to reload all the templates.
Also remember at the top is a Check configuration option which will often pick up this sort of error.

rocky swan
#

ok thanks

#

so i need to keep my oringal code and add yours? correct?

#
template:
 - sensor:
     - name: "Total Lights Count Template"
       state: "{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"
{%- set count = states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count %}
{%- set relays = 
  {
    'switch.switchman_kitchen_left_light': 4,
    'switch.switchman_kitchen_middle_light': 2,
    'swich.relay_3': 4,
    'light.nspanel_kitchen_relay_1': 2
  }
%}
{% set count_relay = relays.items() | selectattr('0', 'is_state', 'on') | map(attribute='1') | sum %}
{{ count + count_relay }}
#

these still show as 1 light

wooden parcel
#

No you need to format everything correctly as TheFes told you and set it to multiline yaml as I told you.

#
template:
 - sensor:
     - name: "Total Lights Count Template"
       state: >- 
         {%- set count = states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count %}
         {%- set relays = 
           {
              'switch.switchman_kitchen_left_light': 4,
              'switch.switchman_kitchen_middle_light': 2,
              'swich.relay_3': 4,
              'light.nspanel_kitchen_relay_1': 2
            }
          %}
          {% set count_relay = relays.items() | selectattr('0', 'is_state', 'on') | map(attribute='1') | sum %}
          {{ count + count_relay }}
#

I recommend you spend some time learning the basics of the syntax so you can understand how to format Yaml files.

rocky swan
#

yes i need to learn this

#

still show as one light

tame seal
#

What does it show on devtools> template

rocky swan
#

template:

  • sensor:
    • name: "Total Lights Count Template"
      state: >-
      0

      0

tame seal
#

Make sure the template is working there first

#

That first lime should not be there

#

You are outputting two counts

#

I see Jane already corrected it

rocky swan
#

This template listens for the following state changed events:

Domain: light
Entity: light.nspanel_kitchen_relay_1
Entity: swich.relay_3
Entity: switch.switchman_kitchen_left_light
Entity: switch.switchman_kitchen_middle_light

wooden parcel
#

So are all those relays on? I worked exactly as I would expect on my setup if I change your devices for some of mine which are switched on.

rocky swan
#

strange... how are you reading the count (the number of light)? I'm using a card on my dashboard using 'total lights count' entity

wooden parcel
#

You need to look at the results on the Dev page where you pasted the template into

rocky swan
#

ok

wooden parcel
#

Once your have it working there then move it to your Template yaml and reload the templates

rocky swan
#

template:

  • sensor:
    • name: "Total Lights Count Template"
      state: "1"

5

#

that relay has 4 lights

#

template:

  • sensor:
    • name: "Total Lights Count Template"
      state: "1"

3

#

making progress but the count is wrong here

#

Reloaded, restarted, doesn’t reflect on the entity card

#

In Dev Tools, the switch with 4 lights show as 5 and the 2 show as 3 so getting an extra 1

wooden parcel
#

You probably will as you have defined the NS panel as a light, so just change it to have 1 on the relay.

#

So obviously it will be counted the in the first count, as you are counting all lights in that one.

#

Check what template you are using the Entity card.

#

I assume you have corrected your template in the yaml and reloaded.

#

Changing the one in Dev tools is just a test environment.

rocky swan
#

Ok thank u. Wld check and confirm

rocky swan
#

thank you so much guys, got it working

#

really appreciate it