#Template-sensor
1 messages · Page 1 of 1 (latest)
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?
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.
Yes thank you
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.
you need a namespace when working with for loops
but with if statements you can use normal variables
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.
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?
an error is too unclear, which arror do you get?
UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'light'
when do you get that?
I’m checking this in dev tools>template
what is your current template?
template:
- sensor:
- name: "Total Lights Count Template"
state: "{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"
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.
Yes this counts my lights at the moment but those on relays as 1
Sorry abt the format
so, when do you get the error?
After pasting the new one in
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.
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
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'
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
Oh I see
Why would we reject light groups?
Current code does not count light groups
because if you don't reject them, it will count the individual lights and the light group itself
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
yes, because it had this: rejectattr('attributes.entity_id', 'defined')
Ok got it
but you changed that to rejectattr('attributes.light.switchman_kitchen_left_light', 'defined')
which broke it
So use the code u gave without adding anything?
what does this return?
and what is the state of sensor.relay?
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?
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)
{%- 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
I’ll confirm. Thank u so much
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 }}
I’m getting this error with both codes, probably my formatting.
Think I got it
Unfortunately, it still counts the light as 1
That’s this one
What's your current code
I copied and pasted this only changing the switch.relay
Please paste it anyway
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 }}
So you say that if you toggle those switches, the output changes with 1 and not with 4 or 2
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 | {
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.
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
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.
What does it show on devtools> template
template:
- sensor:
-
name: "Total Lights Count Template"
state: >-
00
-
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
at the moment it works as it did before, relays only count as 1
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
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.
strange... how are you reading the count (the number of light)? I'm using a card on my dashboard using 'total lights count' entity
You need to look at the results on the Dev page where you pasted the template into
ok
Once your have it working there then move it to your Template yaml and reload the templates
template:
- sensor:
- name: "Total Lights Count Template"
state: "1"
- name: "Total Lights Count Template"
5
that relay has 4 lights
template:
- sensor:
- name: "Total Lights Count Template"
state: "1"
- name: "Total Lights Count Template"
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
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.
Ok thank u. Wld check and confirm