How to improve sensor, part 2

This time reaching out for help is based on my previous question, https://community.home-assistant.io/t/how-to-improve-sensor/150832/14

Unfortunaltey I have one other issue with the sensors with the given data set.

value_template: >-
          {% if ('{{ states.sensor.oekofen.attributes["ww1"]["L_pump"] }}', 'true') %}
            an
          {% else %}
            aus
          {% endif %}

is working fine (result going to correct if / else branch), but is using states.sensor

With the suggestions made by Burningstone and the others above I came up with this version

value_template: >-
          {% if is_state_attr('sensor.oekofen', 'ww1'['L_pump'], 'true') %}
            an
          {% else %}
            aus
          {% endif %}

it is not giving any error but the if statement is evaluated to the else brach (off, or false) although the data would suggest the if branch (on, or true)

This version

value_template: >-
          {% if is_state_attr(('sensor.oekofen', 'ww1')['L_pump'], 'true') %}
            an
          {% else %}
            aus
          {% endif %}

gives the result unknown.

This version

value_template: >-
          {% if is_state_attr('sensor.oekofen', 'ww1'.['L_pump'], 'true') %}
            an
          {% else %}
            aus
          {% endif %}

throws errors when checking the config:

    - Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['sensors']['oekofen_hk1_pump']['value_template']. Got "{% if is_state_attr('sensor.oekofen', 'hk1'.['L_pump']
, 'true') %}\n  an\n{% else %}\n  aus\n{% endif %}". (See ?, line ?).

This one

 value_template: >-
          {% if is_state_attr('sensor.oekofen', 'ww1[L_pump]', 'true') %}
            an
          {% else %}
            aus
          {% endif %}

is also evaluation to the wrong branch (off).

Looks like I really still have no clue on these JSON things :pensive:

Hoping for some more help.

Thanks upfront
Ralf

Based on this from the other thread:

{{ (state_attr('sensor.sensor_name', 'system')['L_ambient'] | int) *0.1 | round(1) }}"

and if the data returned is in the same format as there and the ā€œL_pumpā€ value is true/false then this should work:

value_template: >-
  {% if state_attr('sensor.oekofen', 'ww1')['L_pump'] == 'true' %}
    an
  {% else %}
    aus
  {% endif %}

Thanks finity, keeping it more simple was way better than my trials.

Just had to play around with the quotes for the ā€˜trueā€™ value comparison.
Final working version:

value_template: >-
          {% if state_attr('sensor.oekofen', 'ww1')['L_pump'] == "true" %}
            an
          {% else %}
            aus
          {% endif %}

As the sensor returns true/false you can simplify it even further:

value_template: >-
          {% if state_attr('sensor.oekofen', 'ww1')['L_pump'] %}
            an
          {% else %}
            aus
          {% endif %}
1 Like

tom_I, you are right. I was not sure as I thought it would deliver a string and not a true/false value.

Working smooth :slight_smile:

1 Like

Thereā€™s a way to do it on a single line as well that @123 showed a while ago but I cant find the bookmark so this might not be correct:

value_template: "{% 'an' if state_attr('sensor.oekofen', 'ww1')['L_pump'] else 'aus' %}"

Some of you guys are doing really cool stuff!

Unfortunaltey my programming (and later on re-reading) capabilities are binding me to the ā€˜old styleā€™ if then else structure.

Ha,ha, I learned to programm in Basic on my old C64 almost 40 years back :joy:

Ooooo look at Mr fancy pants with his C64. I had to make do with a Vic20! :nerd_face:

actually it was my brothers C64 as I was far too young to get something like that at the age of 9.
But after some years I did some programming in assembler including an extension for graphical commands.
Oh lord, long, long time back and so much knowledge down the gutter meanwhile

1 Like

Nope, that looks ā€˜spot onā€™ to me (so weā€™d both be wrong !) :+1:

And I had a Texas Instruments Programmable Calculator, used to play parabolic shell game with it to ā€œhit a tankā€

1 Like

Change:
{% ... %}
to:
{{ ... }}


FWIW, I still have my Texas Instruments SR-50 in its soft-case (and its AC adapter). Donā€™t know if it still works because attempting to power it might be the killing blow. I prefer to think it still works ā€¦

First minicomputer I ever programmed was a Data General Nova. The concept of ā€˜bitsā€™ is easily grasped when each bit is an individual toggle switch on the front panel. Programming is very ā€˜tangibleā€™ when you have to flip 16 switches up (1) or down (0), depending on what command you want, then pressing the load button to store it. The whole experience is about as ā€˜close to the metalā€™ as computer programming can get (and slow).

1 Like