Automation with choose for a table with values between X and Y, resulting in an action with value Z

I can’t get this automation to work. When I check it simply does not exist. Logs don’t show errors.

Is there something wrong with the template after the condition?

The idea is:
when sensor.pv_zonder_home_verbruik_ex_boiler is between X and Y, brightness should be Z
I have a table of 30 rows.
If there is a way to do this more easily, ideas are welcome.

X, Y, Z
0, 350, 0
350, 400, 110
400, 450, 112
450, 500, 115
500, 600, 119
...
3000, 3150, 198
3150, 3300, 216
3300, 9999, 235
automation:
- alias: Boiler weerstand dynamisch
  trigger:
    - platform: time_pattern
      seconds: "/10"
  mode: restart
  action:
    - choose:
        - conditions:
            - condition: '{{ states("sensor.pv_zonder_home_verbruik_ex_boiler") | float(0) < 350}}'
          sequence:
            - service: light.turn_on
              data:
                entity_id: light.flush_dimmer_0_10v_boilerweerstand
                brightness: 0
        - conditions:
            - condition: '{{ states("sensor.pv_zonder_home_verbruik_ex_boiler") | float(0) > 349 and states("sensor.pv_zonder_home_verbruik_ex_boiler") | float(0) < 400}}'
          sequence:
            - service: light.turn_on
              data:
                entity_id: light.flush_dimmer_0_10v_boilerweerstand
                brightness: 110
# I have additional conditions for values above 400, until brightness is 235
      default: []

You are checking the state of a sensor and when you write ‘I have a table of 30 rows’ … I guess these values are not in the state of the sensor as this is limited to 256 char. Suggest post the details of the sensor too

The sensor is displaying correct results

sensor:
  - platform: template
    sensors:
      pv_zonder_home_verbruik_ex_boiler:
        friendly_name: Huidig verbruik
        value_template: "{{ (( states['sensor.flukso_pv_vermogen'].state|int(0) - ( states['sensor.fluvius_netto'].state|int(0) + states['sensor.flukso_pv_vermogen'].state|int(0) - states['sensor.batterij_power'].state|int(0) - states['sensor.boiler_power'].state|int(0) ) ) / 2)  | round(0) }}"
        device_class: power
        unit_of_measurement: W
        icon_template: mdi:gauge

So what you are saying is that the STATE of the sensor shows 30 rows of data, each row 3 values? Can you print the sensor itself, not its config?

No, sorry for being unclear.

The sensor has only a value of (eg) 370.
This is between 350 and 400, so I want to set brightness of the light-entity to 110.
When the sensor has a value of 520, I want to set the brightness of the light-entity to 119.
The table is only in a google-sheet :slight_smile:

Since I don’t have an idea on how to create a nice and polished automation my idea was to create an automation with choose in the action, and repeat that 30 times for each of the values, but even that is not working. The automation is not being created (probably because of an error or typo ?).

Did you use the GUI for creating the automation?
This is quite straightforward
EDIT: lets wait till didgerydrew answers… he is more advanced than me :slight_smile:
EDIT2: dd stopped typing so might not respond…so repeating, did you use the GUI to try and create the automation?

No, I did not use the GUI. I never did. I have tons of automations. Most of them are copy/paste from other automations. But I constantly try to find other ways to do different things, here in the community, just to learn, make things simpler, …

Suggest tp try the gui as things have changed … I tried to replicate a bit what you did and found a few differences in the config. The gui will also help to save a proper automation

First, is triggering every 10 seconds absolutely necessary? Short cycle time pattern triggers are rarely the best way to trigger an automation. They require more processing power and often don’t add anything in terms of accuracy or reliability. If your goal was to throttle how often the light is updated when the boiler temp is changing rapidly, you can do that with a condition.

Second, you can use templating to find the current range the temperature is in and relate that to a brightness level.

automation:
- alias: Boiler weerstand dynamisch
  variables:
    boiler: '{{ trigger.to_state.state | float(0) }}'
    brightness: >
      {% set mapper = {
      0 <= boiler < 350: 0,
      350 <= boiler < 400: 110,
      400 <= boiler < 450: 112,
      450 <= boiler < 500: 115,
      500 <= boiler < 600: 119,
      ...
      3000 <= boiler < 3150: 198,
      3150 <= boiler < 3300: 216,
      3300 <= boiler < 9999: 235
      }%}
      {{ mapper.get(True) }}
  trigger:
    - platform: state
      entity_id: sensor.pv_zonder_home_verbruik_ex_boiler
      to: null
  condition: 
    - "{{ now() - this.attributes.last_triggered >= timedelta(seconds = 10) }}"
  action:
    - service: light.turn_on
      data:
        entity_id: light.flush_dimmer_0_10v_boilerweerstand
        brightness: "{{ brightness }}"
  mode: restart

FYI, the last_triggered condition will need to be modified if you are using it in a brand new automation that has never been triggered.

1 Like

Have a look at @Didgeridrew response and this is what I quickly generated with the GUI…of course with my (!) sensors

alias: Demo
description: ''
mode: single
trigger:
  - platform: time_pattern
    seconds: '10'
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.desktop_gaming_memory_available
            below: '350'
        sequence:
          - type: turn_on
            device_id: 9c754e04f8cd5cba3e7f486297e6333c
            entity_id: light.living_room_right_color
            domain: light
            brightness_pct: 10
      - conditions:
          - condition: numeric_state
            entity_id: sensor.desktop_gaming_memory_available
            above: '350'
            below: '900'
        sequence:
          - type: turn_on
            device_id: 9c754e04f8cd5cba3e7f486297e6333c
            entity_id: light.living_room_right_color
            domain: light
            brightness_pct: 90
    default: []
1 Like

Thank you both!

@vingerha
I know it can be done the way you are proposing, but as I wrote I always try to find different ways to achieve the same. That’s why I tried to do this with a template immediately after condition:
Your solution is 4 lines, where I was looking for a more compact code.

@Didgeridrew his code is very compact, and above all, very readable. Understandable for the “casual programmer”.

I like that very much.

Again, thank you both for a very quick solution. I can start building on this with more conditions.

The idea behind the fact that I was looking for compact code:

  • I was hoping that (since I want this specific automation to run very frequently) I could gain execution time (CPU time). Does compact code result in faster execution? I don’t know.
  • Having only 1 location where my sensor is referenced makes it more easy when making changes to the automation.
  • Making changes to my initial automation could result in typo’s (since I would have to make many edits to the automation).

Let me also explain what this automation will actually do:
I’m charging my home-battery with solar power.
I have a heat pump to warm up the house and to heat up the water.
The COP for heating water is very low (COP is less than 2, the heat pump is close to 15 years old now).
I have an electrical heating installed in the boiler, with a COP of 1 (needed for bringing the water temperature above 60°c every few weeks - security/health reason).
I pay 42ct for my electricity.
I get 16ct for injecting electricity.

What I do now (see the sensor for the calculation) determine how much electricity I want to go to the battery and how much electricity I want to use for heating my boiler (50/50 at the moment).

I have an SSR (Solid State Relay) with a 0-10v input. This input is driven by a Qubino Z-Wave dimmer (0-255), what is not actually a light source, but HA sees it as one.

The electrical heating for the boiler is 3kw (actually consuming 3300w when not connected via the SSR).

I’m now able to redirect the power to the battery and the boiler instead of injecting all to the grid.

To be done:

  • I now have a fixed divider (50% battery, 50% boiler) when calculation the sensor, but what when my battery is full. I must make this also dynamic.
  • I better inject left over power when the boiler is hot enough. (I’ll probably add a second condition next to the time condition in this automation). I have to determine the cut-off point based on occupancy in the house, weather conditions the next day, …
  • adding a few lines to the mapper

Again, I’m very grateful that you two have sought a solution for me.