Condition in an automation avtion

Hello there,
I have the following automation:

alias: set_gree_lower_at_heat
description: When slider value changes writes to a modbus register
trigger:
  - platform: state
    entity_id:
      - input_number.gree_lower_at_heat
condition: []
action:
  - service: modbus.write_register
    data:
      hub: versati3
      address: 18
      slave: 1
      value: "{{ 65536 + states('input_number.gree_lower_at_heat') | int }}"
mode: single

What it does, is sending a value from a numeric slider over Modbus. The problem is:

  1. One cannot send negative values, thus, I am twos complementing the value,
  2. This will work only for negative values, as there is no roll over mechanism for 16-bit values.

What I need is a condition, which would add the 65536 only if the input_number.gree_lower_at_heat is lower than 0. I don’t know how to achieve that in terms of HA syntax in the value: field.

I would appreciate all feedback, also for better solutions!

Can you clarify… Do you not want the actions to run at all if the value is greater than 0 or should they still run, just without the addition?

Sorry, let me clarify. When the slider value is < 0 then:

value: "{{ 65536 + states('input_number.gree_lower_at_heat') | int }}"

When the slider value is >= 0:

value: "{{ states('input_number.gree_lower_at_heat') | int }}"
alias: set_gree_lower_at_heat
description: When slider value changes writes to a modbus register
trigger:
  - platform: state
    entity_id:
      - input_number.gree_lower_at_heat
    not_to:
      - unavailable
      - unknown
action:
  - service: modbus.write_register
    data:
      hub: versati3
      address: 18
      slave: 1
      value: >
        {% if states('input_number.gree_lower_at_heat')|int < 0 %}
          {{ 65536 + states('input_number.gree_lower_at_heat') | int }}"
        {% else %}
          {{ states('input_number.gree_lower_at_heat') | int }}
        {% endif %}
1 Like
{% set number_state = states('input_number.gree_lower_at_heat') | int %}
{% set x = 65536 if number_state < 0 else 0 %}
{{ x + number_state }}
1 Like