Simulated power - lights - template issue

Hi.

Im trying to simulate power usage by lights with a template sensor.
Is it someway I can use/refer to the “entity_id” in the template sensor and use this in the value_template? I have marked the entity id with ** to show where it is used.

This one is not working:

  - platform: template
    sensors: 
      spotter_kitchen_power:
        friendly_name: "Kitchen spotter effekt"
        unit_of_measurement: 'W'
        **entity_id**: light.spotter_kitchen
        value_template: >- 
            {%- set num_lights = 5 -%} 
            {%- set light_eff = 10 -%} 
            {%- set sys_eff = 12 -%} 

            {%- set sys_eff_contrib = sys_eff - light_eff -%} 
            {% if is_state('**entity_id**', 'on') %}
              {{ ((((state_attr('**entity_id**','brightness') | int )/255) * (num_lights*light_eff)) + num_lights*sys_eff_contrib) |float|round(2) }}
            {% else %}
              0
            {% endif %}

Thanks.

  • Bjoern

trigger.entity_id … brain fart

1 Like

Except it’s not an automation it’s a template sensor. :smile:

1 Like

First you don’t need to use the entity_id option. It can figure that out if you just use the entity_id in the template:

  - platform: template
    sensors: 
      spotter_kitchen_power:
        friendly_name: "Kitchen spotter effekt"
        unit_of_measurement: 'W'
        value_template: >- 
            {%- set num_lights = 5 -%} 
            {%- set light_eff = 10 -%} 
            {%- set sys_eff = 12 -%} 

            {%- set sys_eff_contrib = sys_eff - light_eff -%} 
            {% if is_state('light.spotter_kitchen', 'on') %}
              {{ ((((state_attr('light.spotter_kitchen','brightness') | int )/255) * (num_lights*light_eff)) + num_lights*sys_eff_contrib) |float|round(2) }}
            {% else %}
              0
            {% endif %}

Doh!

I’m busy working on some automations to augment 0.96 MQTT HVAC (in order to avoid having to create a custom component of it). I took a break, saw this post, and thought I was looking at yet another automation. I’ll see myself out now … :door:

1 Like

Thanks.

I was actually using this solution yesterday, before I created this discussion.
If this is the only solution, its okey for now.
I was hoping there was someway I could use the entity id, only one time.
Easier to reuse, maintain and debug.

As far as I know, there’s nothing in template sensors that is comparable to the trigger variable used in automations (as @123 was thinking.) If there were, this would be easy. So the trick to using the entity_id once is to make sure it’s done in such a way that the automatic entity_id extraction algorithm (basically a regex expression) will find it. I think this might do it:

  - platform: template
    sensors: 
      spotter_kitchen_power:
        friendly_name: "Kitchen spotter effekt"
        unit_of_measurement: 'W'
        value_template: >- 
            {%- set num_lights = 5 -%} 
            {%- set light_eff = 10 -%} 
            {%- set sys_eff = 12 -%} 

            {%- set sys_eff_contrib = sys_eff - light_eff -%} 
            {% set state = states.light.spotter_kitchen %}
            {% if state and state.state == 'on' %}
              {{ ((((state.attributes.brightness | int )/255) * (num_lights*light_eff)) + num_lights*sys_eff_contrib) |float|round(2) }}
            {% else %}
              0
            {% endif %}

I’m pretty sure that having the string states.light.spotter_kitchen is enough for it to find light.spotter_kitchen, so it will know to listen for state changes in that entity.