Using templates in script and modbus.write_register

Newbie problem
How to use templates in the following situation. Have this UI-generated script:
‘’’
modbustx:
alias: modbustx
sequence:

  • service: modbus.write_register
    data:
    address: 5
    unit: 1
    value: 200
    hub: hub1
    mode: queued
    max: 10
    ‘’’
    Wanted solution is to use a template something like
    ‘’’
    modbustx:
    description: “Send set point offset to temperature controller 1”
    sequence:>
    {% if (is_state(“input_boolean.ibstat1”, “on”) and is_state(“input_boolean.ibstat2”, “off”)) -%}
    • service: modbus.write_register
      data_template:
      address: 3
      unit: 1
      hub: hub1
      value: “{{ input_number.inboost }}”
      {%- endif %}

{% if (is_state(“input_boolean.ibstat1”, “on”) and is_state(“input_boolean.ibstat2”, “on”)) -%}

modbus.write_register

{%- endif %}

{% if (is_state(“input_boolean.ibstat1”, “off”) and is_state(“input_boolean.ibstat2”, “on”)) -%}

modbus.write_register

{%- endif %}

{% if (is_state(“input_boolean.ibstat1”, “off”) and is_state(“input_boolean.ibstat2”, “off”)) -%}

modbus.write_register

{%- endif %}

‘’’
Doable ?
I can’t figure out the right syntax

You may have noticed that what you posted above is an illegible mess. Please review guideline 11 in the FAQ: Format it properly.

Code snippets ARE between three back ticks as they should be.
For some reason I was surprised to see what the result looked like. The character I used is ‘’’ before the snippet begins and the same at the end. Seems that ’ character is somehow wrong.

New try with proper back tics
Newbie problem
How to use templates in the following situation. Have this UI-generated script:

modbustx:
  alias: modbustx
  sequence:
  - service: modbus.write_register
    data:
      address: 5
      unit: 1
      value: 200
      hub: hub1
  mode: queued
  icon: mdi:bed
  max: 10

Wanted solution is to use a template something like

modbustx:
description: “Send set point offset to temperature controller 1”
sequence:>
{% if (is_state(“input_boolean.ibstat1”, “on”) and is_state(“input_boolean.ibstat2”, “off”)) -%}
    service: modbus.write_register
    data_template:
    address: 3
    unit: 1
    hub: hub1
    value: “{{ input_number.inboost }}”
    {%- endif %}

What is the right syntax ?

Use an automation…

You don’t need to use a template for that. Just add a condition to your script’s sequence.

There’s no need to create an automation. Automation actions are basically just scripts.

Still fighting with this one. For a pro this must be a piece of cake.
Entity input_number.tupa_korotus is 9.0 at the moment and both booleans are ‘on’.
Have tried just about everything but still no go.
What’s wrong with this script snippet ?

BTW If the code doesn’t show as it should I’m writing this with the last version of Firefox.
I am sure the three Grave Accent characters are OK but still I get the warning of missing backticks.
Is there something I should know about FF when posting ?

alias: modbustx
variables:
  valuetosend: >
    {% if (is_state('input_boolean.ttupakotona', 'on') and is_state('input_boolean.ttupakorotus','on')) %} 
    '{{ input_number.tupa_korotus * 10 }}'  
    {% endif %}
sequence:
  - service: modbus.write_register
    data_template:
      address: 5
      unit: 1
      hub: hub1
      value: '{{ valuetosend | int }}'
mode: single

What’s wrong is the template for the valuetosend variable.

  • It contains a logic error. If neither of the two input_booleans is on it produces no value.
  • It contains a syntax error. If you want the value of an entity, like input_number.tupa_korotus, you should use the states() function to get it.

This version corrects the logic and syntax errors.

  valuetosend: >
    {% if (is_state('input_boolean.ttupakotona', 'on') and is_state('input_boolean.ttupakorotus','on')) %} 
      {{ states('input_number.tupa_korotus') | float(0) * 10 }}
    {% else %}
      0
    {% endif %}

In addition, if you use the int filter then you should provide it with a default value. For example: int(0) means if the filter is unable to convert the value to an integer, it will report 0.

All 4 will be handled and they should create different values to valuetosend. I only used the first as an example.
Tried the suggested version but it always produces 0. So there must be something wrong with the sequence as well especially value: item.

Sorryyyy there was a typo. It works now with the revised code. Thank you for your help and happy Xmas & New Year

Glad to hear it resolved the problem. Best wishes to you for the holidays.


Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.