Hey, first time writer here, I hope I picked the right category.
I’m loosing my marbles trying to setup a script to write a modbus register.
For context, I already extensively control our solar inverter and the hot water heater controller via modbus TCP. Almost everything works, even though it was a steep learning curve.
One thing doesn’t want to cooperate for the life of me:
The hot water heater controller has 4 relay outputs that can be set via modbus to Off, Manual On, or Auto. I want to switch between Off and Auto.
This works:
automation:
- id: "automation_smartfox_aout_mode"
alias: "smartfox aout mode"
description: "Set Aout mode"
trigger:
- platform: state
entity_id:
- input_select.set_sf_aout_ctrl_mode
condition: []
variables:
aout_mode_off: [0x0]
aout_mode_manual: [0x1]
aout_mode_auto: [0x2]
action:
- service: modbus.write_register
data_template:
hub: SmartfoxPro2
slave: !secret smartfox_modbus_slave
address: 42206
value: >
{% if is_state('input_select.set_sf_aout_ctrl_mode', "Off") %}
{{aout_mode_off}}
{% elif is_state('input_select.set_sf_aout_ctrl_mode', "Manual") %}
{{aout_mode_manual}}
{% elif is_state('input_select.set_sf_aout_ctrl_mode', "Auto") %}
{{aout_mode_auto}}
{% else %}
{{aout_mode_auto}}
{% endif %}
mode: single
together with an Input select where I can switch between the 3 options.
This doesn’t work:
script:
set_smartfox_aout_mode:
alias: "Set Smartfox Aout Mode"
description: Set the Aout mode of Smartfox
variables:
aout_mode_off: [0x0]
aout_mode_manual: [0x1]
aout_mode_auto: [0x2]
fields:
mode_in:
selector:
select:
options:
- "Off"
- "Manual"
- "Auto"
multiple: false
required: true
description: The Mode to set the Aout to
name: mode_in
sequence:
- service: modbus.write_register
data:
hub: SmartfoxPro2
slave: !secret smartfox_modbus_slave
address: 42206
value: >
{% if mode_in == "Off" %}
{{aout_mode_off}}
{% elif mode_in == "Manual" %}
{{aout_mode_manual}}
{% elif mode_in == "Auto" %}
{{aout_mode_auto}}
{% else %}
{{aout_mode_auto}}
{% endif %}
mode: single
icon: mdi:car-cruise-control
and I don’t get why!
I can switch between all 3 options with the automation. With the script, “Off” just switches to “Manual” - setting to “Auto” with the script works! “Manual” works too! But running the script selecting “Off” sets the output to Manual.
Any hlp appreciated… I might be overlooking something completely obvious but I’m so hopelessly lost right now… Thanks!
- morgantic
1 post - 1 participant