Help using attributes in "above" and "below" scripting

Hello!
I am trying to control my battery depending on weather.
The end result is to not discharge battery if electricity is below daily average since its cheaper to buy from the grid.

Which means if sensor.electricity_price_stenbrottsgatan attribute “price_level” is above or below attribute “avg_price”.

I havent done much outside of the GUI scripting before since I havent needed to, but now I am trying to do something that HA GUI does not support diffing 2 attributes against each other.

Any idea how to make this flow work?
The part which does not work is ofc "Above/Below: “{{ parts }}”.

alias: Solceller - Discharge control
description: ''
mode: single
trigger:
  - type: value
    platform: device
    device_id: 7c55e92786ad7a593c2aa71bcd74261a
    entity_id: sensor.electricity_price_stenbrottsgatan
    domain: sensor
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sensor.solceller_xioqdrhwj2_realtime_power
                below: '2'
              - condition: numeric_state
                entity_id: sensor.electricity_price_stenbrottsgatan
                attribute: price_level
                above: "{{(state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price'))}}"
        sequence:
          - device_id: ff076423060843a775e130cecff60ab6
            domain: number
            entity_id: number.maximum_discharging_power
            type: set_value
            value: 5000
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sensor.solceller_xioqdrhwj2_realtime_power
                below: '2'
              - condition: numeric_state
                entity_id: sensor.electricity_price_stenbrottsgatan
                attribute: price_level
                below: "{{(state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price'))}}"
        sequence:
          - device_id: ff076423060843a775e130cecff60ab6
            domain: number
            entity_id: number.maximum_discharging_power
            type: set_value
            value: 0
    default:
      - device_id: ff076423060843a775e130cecff60ab6
        domain: number
        entity_id: number.maximum_discharging_power
        type: set_value
        value: 5000

You have a set of redundant brackets there.

"{{ state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price') }}"

Remove them and confirm it works in the Template Editor in Developer Tools. Also try removing the outside quotes.

The numeric state condition does not support templates in above: or below:

You will need to use template conditions.

1 Like

Ok, Do you have time to help a noob out how I would do that looking at my script above? =)

My thinking was this but dosent seem to be correct:

condition: template
value_template: '{{state_attr('sensor.electricity_price_stenbrottsgatan', 'price_level') > state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price')}}'

So in theory:

alias: Solceller - Discharge control
description: ''
mode: single
trigger:
  - type: value
    platform: device
    device_id: 7c55e92786ad7a593c2aa71bcd74261a
    entity_id: sensor.electricity_price_stenbrottsgatan
    domain: sensor
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sensor.solceller_xioqdrhwj2_realtime_power
                below: '2'
              - condition: template
                value_template: >-
                  {{ (state_attr('sensor.electricity_price_stenbrottsgatan', 'price_level')|float)
                  > (state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price')|float) }}
        sequence:
          - device_id: ff076423060843a775e130cecff60ab6
            domain: number
            entity_id: number.maximum_discharging_power
            type: set_value
            value: 5000
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sensor.solceller_xioqdrhwj2_realtime_power
                below: '2'
              - condition: template
                value_template: >-
                  {{ (state_attr('sensor.electricity_price_stenbrottsgatan', 'price_level')|float)
                  < (state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price')|float) }}
        sequence:
          - device_id: ff076423060843a775e130cecff60ab6
            domain: number
            entity_id: number.maximum_discharging_power
            type: set_value
            value: 0
    default:
      - device_id: ff076423060843a775e130cecff60ab6
        domain: number
        entity_id: number.maximum_discharging_power
        type: set_value
        value: 5000

Close. If your attributes aren’t numbers (if they are strings that look like numbers) then you have to convert them. Also you need to use double quotes outside your template or you get this:

condition: template
value_template: '{{state_attr('

So if your attributes are numbers:

condition: template
value_template: "{{ state_attr('sensor.electricity_price_stenbrottsgatan', 'price_level') > state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price') }}"

if they aren’t:

condition: template
value_template: "{{ state_attr('sensor.electricity_price_stenbrottsgatan', 'price_level')|float(0) > state_attr('sensor.electricity_price_stenbrottsgatan', 'avg_price')|lfoat(0) }}"

Ok nice!
Thx, will use this for reference learning more.

BUT, to my current problem I noticed price_level is not a FLOAT but a WORD :upside_down_face:
So I first need to make sure I am fetching what I want.

I still get this however testing 2 values I now know both are floats.
I also tried adding |float(0).

Development area tells me they have this data:

max_price: 1.374
avg_price: 0.513
min_price: 0.054
template value should be a string for dictionary value @ data['value_template']. Got None

Posting relevant parts only and leaving actions out:

action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sensor.solceller_xioqdrhwj2_realtime_power
                below: '2'
              - condition: template
                value_template: >-
                  {{ state_attr('sensor.electricity_price_stenbrottsgatan',
                  'min_price') >
                  state_attr('sensor.electricity_price_stenbrottsgatan',
                  'avg_price') }}
        sequence:
          - device_id: ff076423060843a775e130cecff60ab6
            domain: number
            entity_id: number.maximum_discharging_power
            type: set_value
            value: 5000

That’s actually an issue with the automation editor, not your template:

Yeah that made sense.
got it working now, thx for all the help =)

Hi everyone,

I am having a similar issue with attributes. I am a noob, so please be kind.

I am trying to create a binary sensor that listens to my device tracker attribute “speed”.

I want this to show True when speed is 30.0 or over.

This binary sensor will be a Driving Sensor. Moving or Not Moving.

I have got this far.

{% if states.device_tracker.bobby_cuthill %}
  {{ state_attr('device_tracker.bobby_cuthill', 'speed') | float > 30.0 }}
True
{% else %}
False
{% endif %}

Thanks in advance.

Try this:

template:
  - binary_sensor:
      - name: High Speed
        state: "{{ state_attr('device_tracker.bobby_cuthill', 'speed') | float(0) > 30 }}"
        availability: "{{ state_attr('device_tracker.bobby_cuthill', 'speed') | is_number }}"
        device_class: problem

The greater than test returns true or false for you.

The default (0) of the float filter prevents the template from not loading if the attribute is unavailable.

The availability template marks the sensor as unknown in this case.

You can pick an appropriate device class here if that one is not suitable https://www.home-assistant.io/integrations/binary_sensor/#device-class

That worked perfectly! I had the impression every available state of the entity needs to have an IF statement. I understand a little bit more today!

Thanks a lot for your help. Really appreciate it.

Hi!

I have same problem. I read the forum and don’t work. i have two AC and small fuse. i’'m waiting a bigger from service provider but now the two AC can’t run in same time. i’m try to create multiple automation to control it. I’m create the automation in Node red, but the node red is crashed some times and the house is cooled out. i trying to create a condition, get the setted temperature and the measured temperature from the climate, and if the curr < sett then turn the AC in heat mode.

this is my template condition:

condition: template
value_template: "{{ state_attr('climate.c63aaba3', 'current_temperature') |float(0) < state_attr('climate.c63aaba3', 'temperature') |float(0) }}"

but the condition never match. Why? And have any mode to debug this? (inspecting the returned attributes somehow?) and what is equal? (= or == or what?)

i tryed:
|float
|int
and without these.

Or i’m use the wrong condition?

This is the currently running (now disabled) node red float:

i want to do this working until the service provider not done the work.

Thanks