Hello
Got it working now I think though but feel that a large part of my leds are red even if I test with less cost. Maybe it should be like that, but there is only one joint that is green and one that is yellow, the rest are red.
when I post it in a template I get the error: UndefinedError: âtriggerâ is undefined so something is probably wrong.
alias: Elpris Ledmätare
description: Visa elkostnad
trigger:
- platform: state
entity_id:
- sensor.nordpool
action:
- service: rest_command.wled_update
data:
payload: "{\"on\": true}"
- service: rest_command.wled_update
data:
payload: >-
{##### INPUTS #####} {# Use the value of the entity that has triggered
the automation #} {% set sensor_entity_id = trigger.entity_id %} {% set
sensor_entity_id = "sensor.nordpool" %} {# Value that should fill the
complete meter #} {% set max_value = 5 %} {# Number of leds in the
strip #} {% set led_count = 20 %} {##### CALCULATION #####} {# Fraction
of the meter that should be on #} {% set sensor_value =
states(sensor_entity_id) | float(default=0) %} {% set fraction =
sensor_value / max_value %} {% set brightness = 100 %} {# LED parts:
normal for the active bar, then highlight the exact value, then lowlight
the inactive part #} {% set highlight_end_leds = (fraction * led_count)
| int %} {% set normal_end_leds = highlight_end_leds - 3 %} {% set
lowlight_start_leds = highlight_end_leds %} {# Resulting segments to
send to WLED #} {# - Start with turning everything off #} {% set result
= namespace(segments=[0, led_count, [0,0,0]]) %} {##### HELPERS #####}
{# Add a segment, while a possible scaledown in brightness #} {# -
Reversed start/end because strip is upside down #} {% macro
segment(start_led, end_led, color, scaledown=3) -%}
{%
set result.segments = result.segments + [
led_count - end_led,
led_count - start_led,
[
(color[0] / scaledown) | round(0, 'ceil'),
(color[1] / scaledown) | round(0, 'ceil'),
(color[2] / scaledown) | round(0, 'ceil'),
]
]
%}
{%- endmacro %} {# List of color blocks, references sensor values #} {%
set blocks = [
{"start": 0, "end": 0.3, "color": [0, 255, 0]},
{"start": 0.3, "end": 0.5, "color": [255, 255, 0]},
{"start": 0.5, "end": 1.5, "color": [255, 102, 0]},
{"start": 1.5, "end": max_value, "color": [255, 0, 0]},
]
%} {% for block in blocks %}
{# Convert sensor values of the block to led counts #}
{% set block_start_leds = (block.start / max_value * led_count) | round %}
{% set block_end_leds = (block.end / max_value * led_count) | round %}
{# NORMAL segment #}
{% if normal_end_leds >= block_start_leds %}
{{
segment(
[block_start_leds, 0] | max,
[block_end_leds, normal_end_leds] | min,
block.color,
scaledown = 1
)
}}
{% endif %}
{# HIGHLIGHT segment: If the highlight overlaps this block, add a highlight #}
{% if ([block_start_leds, normal_end_leds] | max) <= ([block_end_leds, highlight_end_leds] | min) %}
{{
segment(
[block_start_leds, normal_end_leds] | max,
[block_end_leds, highlight_end_leds] | min,
block.color,
scaledown = 1
)
}}
{% endif %}
{# LOWLIGHT segment: when a part of this block is inactive #}
{% if lowlight_start_leds <= block_end_leds %}
{{
segment(
[block_start_leds, lowlight_start_leds] | max,
[block_end_leds, led_count] | min,
block.color,
scaledown = 2
)
}}
{% endif %}
{% endfor %} {# Print results, passing it to the rest command #} {# -
Use full segment brightness, but lower strip brightness #} {# - Turn
on/off based on living room usage #} {{
{
"bri": brightness,
"seg": {
"bri": 255,
"i": result.segments
}
}
}}
mode: single