Hello Everyone. I was wondering if someone could help me finish this code. Im a little stuck. I need to write a value to a modbus register on my home battery system I have built. Postive number tell it to charge and negative numers to discharge. Problem is I cannot write negative numbers easily. Positive number are fine no issue. I have to write my values via the 2 compliment method. The scale is 0-32768 are postive and 65536 - 32768 are negative numbers. For example if I wish for my battery to discharge by -100W its 65536 - 100 = 65436 = -100.
All I want my statement to say. If the houseload is between 0 and 3000W then write that value to the template output as positive numbers are not an issue. However IF the house load is between -1 to -3000W then 65536 + (-house load). If its anything else the write + 40W. This the code I have so far.
- platform: template
sensors:
grid_step:
friendly_name: “Grid Setpoint to Battery”
value_template: >-
{% if states(‘house load’) | float > 0 and states(‘house load’) | float < 3000 %}
{{states(’???’) | int}}
{% elif states(‘house load’) | float > -1 and states(‘house load’) | float < -3000 %}
{{ 65536 + states(’-???’) | int}}
{% else %}
40
{% endif %}
I think im on the right track but im a novice and writing code.
Any help would be much appreciated.
Thanks
Gareth