Hi,
I am struggling to get my peak shaving script working. What I try to do is sort a list of electricity consumers in an input_text list by the consumption of the entity (that I have got working). Then I want to select the highest consumers of the list and switch it off. Check the total consumption threshold and move on to the next consumer if the threshold is not met. See here the script which works for one thing and that is selecting the entity_id of the consumer to switch it off. If I fill in the id manually it works but the selection from the list doesn’t. In the template tester (Dev Tools) it works but in the actual script it doesn’t. Here is the script. In line 25 it goes wrong (see # ‘{{ consumers[0] }}’ doesn’t work!!!). I have tried a lot but I can’t get it right. Someone an idea?
alias: Power Down
sequence:
- repeat:
while:
- condition: template
value_template: >-
{{(states('sensor.smappee_1107002938_local_total_consumption_active_power')|float
>= states('input_number.scale_down_trigger')|float) }}
sequence:
- service: input_text.set_value
data:
value: >-
{% set consumers = [
{'entity_id': 'switch.sonoff_1000bee815', 'consumption': states('sensor.kw_pool_pump') | float },
{'entity_id': 'switch.sonoff_1000fe1730', 'consumption': states('sensor.sonoff_1000fe1730_power') | float },
{'entity_id': 'switch.sonoff_1000a012cb', 'consumption': states('sensor.kw_ventilator_game_room') | float },
] %}
{% set consumers = consumers| sort(attribute='consumption',
reverse=true) | map(attribute='entity_id') | list %}
target:
entity_id: input_text.consumers
- service: switch.turn_off
data: {}
target:
entity_id: "{{ 'switch.sonoff_1000bee815' }}" # '{{ consumers[0] }}' doesn't work!!!
- service: notify.notify
data:
message: largest consumer stopped
- delay: "00:01:00"
- service: homeassistant.update_entity
entity_id: sensor.smappee_1107002938_local_total_consumption_active_power
- delay:
hours: 0
minutes: 1
seconds: 0
milliseconds: 0
- service: input_text.set_value
data:
value: " "
target:
entity_id: input_text.consumers
icon: mdi:transfer-down
mode: single
Eventually I want to do similar with scaling up in a reversed order and switching them back on. I am preparing this for the max use of solar power and the minimum consumption of grid power within limits.
Obviously I have a syntax issue and I can’t get the entity_id: “{{ consumers[0] }}” working… UndefinedError: consumers not defined, and I can’t figure it out.
There are various way I get it working in the Dev Tools template. No problem at all but in the script itself it doesn’t and I keep getting back the UndefinedError