Traffic light - Multiple values turn RGB red yellow green depending on electric price ( Nordpool )

Hi,
I need help, a basic automation for the family to get an understanding on where the electric price is, to not turn on excessive electric appliances during expensive hours.

Im trying to replicate waltrix watches with HA and lights i already have at hand.
https walltrix dot se

Im trying to learn create advanced automations.
So far im only creating automations with the HA gui witch works great!
But not for more complex stuff :frowning:

I get all values from nordpool integration and get a lot of useful information i can use from that. I dont know how to compare two values and if above/between/under i want to control a rgb light to state red/yellow/green.
I have figured out i need to create a yaml template?

You have seen this a couple of times i guess :slight_smile:

My yaml skills are questionable but i want give it a go.
So my thoughts what i want to achive,

if sensor.nordpool_kwh_se3_sek_2_095_025 current_price
  less then off_peak_2
     set light.bulb_6_multi_color to green
fi
if sensor.nordpool_kwh_se3_sek_2_095_025 current_price
  greater then off_peak_2 &&  less then mean   ( between off_peak_2 & mean )
     set light.bulb_6_multi_color to yellow
fi
if sensor.nordpool_kwh_se3_sek_2_095_025 current_price
  greater then mean
     set light.bulb_6_multi_color to red
fi

And if some one can help me with templates for doing precentages aswell if i want to play around for optimal values and to learn what is hapening when changing stuff would be appritiated.

Thanks in advance!

Regards
Kim

If you want to use templates for this (which I agree is probably the way to go), then go to the developer’s tools table template page and start experimenting. If you haven’t already, you have to activate advanced mode on your profile to see this page.

Here are what your three templates should look like, I think.

# is current less than off peak 2?
{{ states('sensor.nordpool_kwh_se3_sek_2_095_025') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'off_peak_2') }}

# is current between off peak 2 and mean?
{{ states('sensor.nordpool_kwh_se3_sek_2_095_025') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'off_peak_2') 
  and states('sensor.nordpool_kwh_se3_sek_2_095_025') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'mean') }}

# is current greater than mean?
{{ states('sensor.nordpool_kwh_se3_sek_2_095_025') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'mean') }}

You’ll notice I took care of the cases where the current was equal to mean or off peak as well. When you put this in the templates tab, one of them should be True and the others False. Note that if mean is ever less than or equal to off peak, then none of these would be true, so that may be a special case you need to consider.

If the templates are working the way you expect, then you can create an automation with a trigger on this entity state and a Choose building block. The condition for each choose option would be one of these templates.

Or, you could convert this into an if/then/else template and just have one service to turn on the light to a color determined by the template. That would look something like this:

service: light.turn_on
entity_id: light.bulb_6_mult_color
data:
  brightness: 120
  rgb_color: >-
    {% if states('sensor.nordpool_kwh_se3_sek_2_095_025') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'off_peak_2') %}
      [0, 255, 0]
    {% elif states('sensor.nordpool_kwh_se3_sek_2_095_025') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'off_peak_2') 
  and states('sensor.nordpool_kwh_se3_sek_2_095_025') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'mean') %}
      [255, 255, 0]
    {% elif states('sensor.nordpool_kwh_se3_sek_2_095_025') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'mean') %}
      [255,0,0]
    {% else %}
      [0,0,255]
   {% endif %}

This template can also be tested in the developer tools tab. I used blue for the last else to catch any outlier cases. This would be your clue that something is not working right.

Thank you so much!
This was more then i could ask for.

The blue addition to anything else was great!
Why didnt i think of that… :slight_smile:

There were some minor errors, very minor, that i worked around.
But that made me start looking, and pointed me in a direction where to start.
I discovered a lot of things along the way.

At first there was some indentation errors that made me start thinking.
But thats an easy one, happens to all of us :slight_smile: Dont just copy paste :smiley:
That was the easy part. But i appreciate it as it made me start looking.

It first threw some errors i could not understand.
light.turn_on. Error rendering data template: TypeError: '<' not supportedbetween instances of 'str' and 'float'
But i worked around them when the code started to make sense.
insted of states, i did as you did previously, state_attr( …, ‘current_price’)
Worked perfectly. I dont know why states did not work as it reports the current price?
UPDATE: i searched a bit more and converted to float, works aswell
states('sensor.nordpool_kwh_se3_sek_2_095_025'), float(0)

And for some reason i needed to use target:

This made me dig deeper in to the states and values and all sort of stuff.
lights.turn_on has a lot of data parameters i can set!
Thanks for helping me out getting in to more advanced stuff, not just gui klicking.
I understand more now.

This was the finished code i added to get it working in the Developer —> services tab:

service: light.turn_on
target:
  entity_id: light.bulb_6_multi_color
data:
  brightness: 10
  rgb_color: >-
    {% if state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'current_price') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'off_peak_1') %}
      [0, 255, 0]
    {% elif state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'current_price') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'off_peak_1') and state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'current_price') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'mean') %}
      [255, 255, 0]
    {% elif state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'current_price') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025', 'mean') %}
      [255,0,0]
    {% else %}
      [0,0,255]
    {% endif %}

Now i can play around with values i like for the traffic light.
And again thanks for the help getting deeper in to the rabbit hole.

Thanks once again!

The sweet thing is that one automation handles every state, and not creating 3 different automations that does the same thing.
This is great!

Is this the correct way of adding it?
Correct may be wrong word, but optimal way of adding.

Added in automation entity: nordpool.kwh… and changed to YAML editing:

alias: Traffic Light
description: "Change RGB depending on electric prices"
trigger:
  - platform: state
    entity_id:
      - sensor.nordpool_kwh_se3_sek_2_095_025
condition: null
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness: 5
      rgb_color: >-
        {% if state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'current_price') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'average') -%}
          [0, 255, 0]
        {% elif state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'current_price') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'average') and state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'current_price') < state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'peak') -%}
          [255, 255, 0]
        {% elif state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'current_price') >= state_attr('sensor.nordpool_kwh_se3_sek_2_095_025',
        'peak') -%}
          [255,0,0]
        {% else -%}
          [0,0,255]
        {% endif -%}
    target:
      entity_id: light.bulb_6_multi_color
mode: single

This is the result.
Can i improve?

Thank you!