I’m trying to set up a notification including a variable but can’t get it to work.
I have this code below which looks up the cheapest energy slots to charge a solar battery. It works fine but I’d like it to send a notification to me when it runs stating how many slots have been selected (result). I tried various bits of code - I can get it to send a notification - but where the contract of result should be it just shows “”
How do I include the numeric contact of ‘result’ in the message?
service: octopus_energy.update_target_config
target:
entity_id: binary_sensor.octopus_energy_target_night_low_rate_new_sensor
data:
target_hours: >
{%- set result = ((13 - (14 * states('sensor.battery_soc')|float) / 100) /
3.4)|round(2) %} {%- set charge_period_length = 0.5 %} {%- if (result -
result|int)|round(2) != 0 %}
{%- if (result - result|int)|round(2) != charge_period_length %}
{%- if (result - result|int)|round(2) < charge_period_length %}
{%- set result = (result|int + charge_period_length)|float %}
{%- else %}
{%- set result = (result|int + 1)|float %}
{%- endif %}
{%- endif %}
{%- endif %} {%- set charge_periods = (result / 0.5)|float %}
"{{result|string}}"
right now it’s returning a quoted string. is that what octopus_energy.update_target_config requires?
ie, it returns something like
"4.0"
instead of 4.0
is that causing the service to reject it? if so, remove the " on the last line.
i gotta say, the code is also a bit complicated. and some things there don’t make sense to me. like… why are you setting charge_periods at the end? that gets tossed unless you have more template that you’re not showing (not more yaml… that variable will die at the end of this target_hours scope.
and having 3 nested if statements almost always screams out “gotta be a better way”
The part quoted is working fine - although I’m no coder (just got HA a couple of weeks back) so not claiming it’s the best. But it does work. I just want to add a notification at the end which reports the number of charge periods created (result). I can get it to send the notification, but whatever I try I can’t get it to include the numeric content of result in the notification. I’ve tried with and without quites and it makes no difference. It’s just shows “” in the notification message . Any ideas?