Return numeric value to Modbus Coil using script

Hi.

I am having problems making my “auto / manual” function work. if I connect input_select.automanlysbag from configuration.yaml directly to value in “manuellysbag” in automation.yaml then it works. But when I try to do it with a script, I get no data in my BMS system.

I would like the end result to be like this:
(Off)Sluk = 0
(On)Tænd = 1
(Auto)Automatik = 2
image

What am I doing wrong?

configuration.yaml

automation: !include automations.yaml
script: !include scripts.yaml

#overstyring af udvendig lys og stik
input_select:
  automanlysbag:
    name: Manuel lys bag
    initial: Automatik
    options:
      - Sluk
      - Manuel
      - Automatik
    icon: mdi:target

automation.yaml:

- id: manuellysbag
  alias: Manuel lys Baghave
  trigger:
    platform: state
    entity_id: script.manselectlysbag
  action:
    service: modbus.write_register
    data_template:
      hub: bms
      unit: 1
      address: 700
      value: "{{ values('script.manselectlysbag') }}"  

script.yaml

manselectlysbag:
    alias: Overstyr lys i baghave
    sequence:
     -  service: input_select.set_value
        data_template:
          entity_id: >
            {% if is_state("input_select.automanlysbag", "Sluk") %} 0
            {% elif is_state("input_select.automanlysbag", "Manuel") %} 1
            {% else %} 2
            {% endif %} 

@Jannik_Schallert Since your script is setting the value of an input_select your automation may work if you use the value of the input select but it also may be that you are not specifying the desired value is an integer in your template?

In either case I use this as my value template in my automation which gets it’s value from an input_number:

value: "{{ states.input_number.slider5.state|int }}"

Hi Wellsy.
-Thanks for your reply
But… that does not work either…

I tried different things to extract the nummeric value from the script. to pass it on to my automation action, but nothing works.

i think the problem should be found in data template in the script, or in my automation action.

I have an input_number.Slider which works fine, it was “easy”, I did not have to convert a “string” to a “numeric” value like here…

I’ve gotten a little further, now it returns the value ‘0’ when the input_select is trigged…
I do not understand where it gets “0” from, I’v tried to replace ‘0’ with ‘2’ in the script, so it should not be returning ‘2’, but it is still returns ‘0’ to MODbus

automations.yaml


- id: manuellysbag
  alias: Manuel lys Baghave
  trigger:
    platform: state
    entity_id: input_select.automanlysbag
  action:
    - service: script.turn_on
      entity_id: script.manselectlysbag
    - service: modbus.write_register
      data_template:
        hub: bms
        unit: 1
        address: 701
        value: "{{ states.script.manselectlysbag.state|int }}" 

scripts.yaml

manselectlysbag:
  sequence:
  - service: modbus.write_register
    data_template:
      entity_id: >
        {% if is_state("input_select.automanlysbag", "Sluk") %} 0
        {% elif is_state("input_select.automanlysbag", "Manuel") %} 1
        {% elif is_state("input_select.automanlysbag", "Automatik") %} 2
        {% endif %} ```

@Jannik_Schallert Ok…Have you tested your value template you included in your script in the template editor? If yes…Is that giving you the expected values?

aaarh…
I didn’t know there was such a feature. I have only messed with HASS for a month :slight_smile:
Now it works.
i removed my script, and made it in automations.yaml instead
value: '{% if is_state(“input_select.automanlysbag”, “Sluk”) %} 0 {% elif is_state(“input_select.automanlysbag”, “Tænd”) %} 1 {% else %} 2 {% endif %}'

- id: manuellysbag
  alias: Manuel lys Baghave
  trigger:
    platform: state
    entity_id: input_select.automanlysbag
  action:
    - service: modbus.write_register
      data_template:
        hub: bms
        unit: 1
        address: 701
        value: '{% if is_state("input_select.automanlysbag", "Sluk") %} 0 {% elif is_state("input_select.automanlysbag", "Tænd") %} 1 {% else %} 2 {% endif %}'
1 Like