cmd
(Gustav)
May 7, 2022, 6:49am
1
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
zoogara
(Daryl)
May 7, 2022, 7:00am
2
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.
tom_l
May 7, 2022, 7:41am
3
The numeric state condition does not support templates in above:
or below:
You will need to use template conditions.
1 Like
cmd
(Gustav)
May 7, 2022, 8:04am
4
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
tom_l
May 7, 2022, 8:18am
5
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) }}"
cmd
(Gustav)
May 7, 2022, 8:20am
6
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
So I first need to make sure I am fetching what I want.
cmd
(Gustav)
May 7, 2022, 8:53am
7
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
tom_l
May 7, 2022, 8:56am
8
That’s actually an issue with the automation editor, not your template:
opened 01:22PM - 08 Apr 22 UTC
### The problem
In order to implement a somewhat more complex template conditio… n, I came across the problem that (at least testing) templates conditions do not work (anymore).
What I did:
Create a simple automation having a template condition:
```
- condition: template
value_template: "{{ 1 > 2 }}"
```
So, this is extremely simplifed. The expression should return False, but instead I get the following error when testing this expression in the condition frontend (under automatisations):
`Error handling message: template value should be a string for dictionary value @ data['value_template']. Got None (invalid_format)`
If I enter the same snippet of code in the templates section un der developer tools, it prints false.
Either there is a problem in the code just adding the template condition in the automatisation frontend or something is completely borked.
### What version of Home Assistant Core has the issue?
2022.4.1
### What was the last working version of Home Assistant Core?
_No response_
### What type of installation are you running?
Home Assistant Container
### Integration causing the issue
Automation
### Link to integration documentation on our website
_No response_
### Diagnostics information
_No response_
### Example YAML snippet
```yaml
- condition: template
value_template: "{{ 1 > 2 }}"
```
### Anything in the logs that might be useful for us?
```txt
2022-04-08 14:40:54 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140209354674720] Error handling message: template value should belue_template']. Got None (invalid_format)
```
### Additional information
_No response_
cmd
(Gustav)
May 7, 2022, 1:14pm
9
Yeah that made sense.
got it working now, thx for all the help =)
Boabee
(Boabee)
October 3, 2022, 3:47am
10
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.
tom_l
October 3, 2022, 3:53am
11
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
Boabee
(Boabee)
October 3, 2022, 4:20am
12
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.
abt
(aht)
November 10, 2022, 10:41am
13
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