I confess I am a “copy and paste” coder which acknowledges that I don’t really understand YAML and HA coding. But I am hoping someone can point me in the right direction for an annoying issue I can’t get to the bottom of.
For background, I have an automation to charge my Tesla from excess solar. I have setup a template sensor that does the math on my grid export to figure out how much current I have free to charge. And then I have an automation that runs a script and sets the charge rate to use only excess solar. That all is working relatively well, but effectively it is setting the charge rate every 2 seconds which is probably excessive. So I would like to change that slightly, so I only change the charge rate, if the charge rate needs to changed because it has changed, which it mostly does not need to (charge rate is an integer from 0 to 15Amp, there is only 15 options and it will spend most of the day at 15A). So I have changed the “Action” in the auomation to execute and IF statement. But it is not working for me.
The crux of what I want to do is if sensor.freesolar_tesla_charge_current_fixed != number.greyghost_charging_amps then run script.tesla_set_charge_rate_v2_5.
My automation is as following :-
alias: GreyGhost - Charge with excess solar while at home during daylight v2
description: ""
trigger:
- platform: time_pattern
seconds: /2
condition:
- condition: state
entity_id: device_tracker.greyghost_location_tracker
state: home
- condition: sun
before: sunset
after: sunrise
before_offset: "00:00:00"
after_offset: "00:40:00"
action:
- if:
- condition: template
value_template: >-
"{{ states('sensor.freesolar_tesla_charge_current_fixed' ) !=
states('number.greyghost_charging_amps' ) }}"
then:
- service: script.tesla_set_charge_rate_v2_5
data: {}
mode: single
But this never executes the script.tesla_set_charge_rate_v2_5 script. From a look at the trace from the automation seems to always evaluate :-
"{{ states('sensor.freesolar_tesla_charge_current_fixed' ) !=
states('number.greyghost_charging_amps' ) }}"
as false. This is despite the sensor.freesolar_tesla_charge_current_fixed and number.greyghost_charging_amps being different values.
What is even more confusing, if I paste the following it “Developer Tools \ Template” to try and fix it, it confirms the values are different AND in Developer Tools, it correctly evalutes the condition as “True” as the values are NOT EQUAL:-
freesolar_tesla_charge_current_fixed : "{{ states('sensor.freesolar_tesla_charge_current_fixed' ) }}"
number.greyghost_charging_amps : "{{ states('number.greyghost_charging_amps' ) }}"
"{{ states('sensor.freesolar_tesla_charge_current_fixed' ) != states('number.greyghost_charging_amps' ) }}"
"{{ states('sensor.freesolar_tesla_charge_current_fixed' ) != states('number.greyghost_charging_amps' ) }}"
"{{ states('sensor.freesolar_tesla_charge_current_fixed' ) | int != states('number.greyghost_charging_amps' ) | int }}"
"{{ states('sensor.freesolar_tesla_charge_current_fixed' ) | string != states('number.greyghost_charging_amps' ) | string }}"
If fact you can see I tried different things to fix the types etc (stumbling around in the dark guessing because of my lack of knowledge), but all the combinations correctly evaluated.
So my issue is the evaluation works exactly as I would expect in Developer Tools, but not in the automation. So I suspect there is something basic about this that I am completely overlooking.
For reference “sensor.freesolar_tesla_charge_current_fixed” is I Template Sensor I have created, and number.greyghost_charging_amps is something created my the Tesla integration and is something I use in the script to set the charge rate. But I could be missing something about how these can and can’t be used.
There is some subtilty of this I am missing, and I am hoping someone here can help me with this.
Thanks very much in advance.