jcwillox / hass-template-climate

❄️Templatable Climate Device for Home Assistant, Supports Running Actions On Service Calls.
MIT License
133 stars 38 forks source link

Error "running script requires passing in a context" | Fancoil Modbus Integration #44

Open carl-bb opened 1 year ago

carl-bb commented 1 year ago

The problem

Hello,

I have fancoil wired in a modbus rs485 serial network. I want to use hass-template-climate component because i have to do math with some modbus registers and the standard modbus climate component doesn't allow to do that and also lack of the fan_mode functionality.

I have set all as in the .yaml attached. all readings are fine but when I try to execute an action like 'set_temperature', 'set_hvac_mode' or 'set_fan_mode' nothing happen and the value is restored to the original one. I have tried to execute the service 'modbus.write_register' on the 'developer tools' tab and all of them work. In the log I see "running script requires passing in a context"

What's going on? How can I fix this?

Thanks

What version of Template Climate has the issue?

0.6.1

What version of Home Assistant are you running?

2023.5.4

What type of installation are you running?

Home Assistant OS

Example YAML snippet

# FANCOIL SOGGIORNO - CLIMATE TEMPLATE
# ------------------------------------

- platform: climate_template
  name: Soggiorno
  unique_id: v1_climate_template
  min_temp: 16
  max_temp: 28
  precision: 0.1
  temp_step: 0.5
  swing_modes:
    - "on"
    - "off"
  modes:
    - "heat"
    - "cool"
  fan_modes:
    - "auto"
    - "low"
    - "medium"
    - "high"

  availability_template: "{{is_state('binary_sensor.fc_soggiorno_com_err','off')}}"

  current_temperature_template: "{{states('sensor.fc_soggiorno_t1')}}"
  target_temperature_template: "{{states('sensor.fc_soggiorno_sp')}}"

  swing_mode_template: "{{iif(is_state('binary_sensor.fc_soggiorno_stdby','off'), 'on', 'off')}}"

  hvac_mode_template: "{{iif(is_state('sensor.fc_soggiorno_man','3'), 'heat', 'cool')}}"

  hvac_action_template: >
    {% if is_state('binary_sensor.fc_soggiorno_mod_raff','on') %}
    heating
    {% elif is_state('binary_sensor.fc_soggiorno_mod_risc','on') %}
    cooling
    {% else %}
    idle
    {% endif %}

  fan_mode_template: >
    {% if is_state('binary_sensor.fc_soggiorno_mod_auto','on') %}
    auto
    {% elif is_state('binary_sensor.fc_soggiorno_mod_notte','on') %}
    low
    {% elif is_state('binary_sensor.fc_soggiorno_mod_min','on') %}
    medium
    {% else %}
    max
    {% endif %}

  set_temperature:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 231
        value: "{{ state_attr('climate.soggiorno', 'temperature') | int * 10}}"

  set_hvac_mode:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 233
        value: "{{iif(is_state('climate.soggiorno','heat'), 3, 5) | int }}"

  set_fan_mode:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 201
        value: >
          {% if is_state_attr('climate.soggiorno','fan_mode', 'auto') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65528) }}
          {% elif is_state_attr('climate.soggiorno','fan_mode', 'low') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65530) | bitwise_or(2) }}
          {% elif is_state_attr('climate.soggiorno','fan_mode', 'medium') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65529) | bitwise_or(1) }}
          {% else %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65531) | bitwise_or(3) }}
          {% endif %}

  set_swing_mode:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 201
        value: >
          {% if is_state_attr('climate.soggiorno','swing_mode', 'on') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_or(128) }}
          {% else %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65407) }}
          {% endif %}

Anything in the logs that might be useful for us?

2023-05-29 01:31:00.709 WARNING (MainThread) [homeassistant.helpers.script.soggiorno] Soggiorno: Running script requires passing in a context

Additional information

No response

Tonguc-Endem commented 1 year ago

Your problem is that when you're calling your services, you're using values from the template-cliame entity. However, at that point the climate entity is not holding the new values you've selected from the card interface. So you're just passing the unchanged value before you change it.

What you need to do is to use the specific variables provided by the template for the appropriate action you want to do.

here is a suggestion:

  set_temperature:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 231
        value: "{{ temperature | int * 10}}"

  set_hvac_mode:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 233
        value: |-
          {%- if hvac_mode ==  'heat' -%}
            3
          {%- else -%}
            5
          {%- endif -%}

  set_fan_mode:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 201
        value: >
          {% if fan_mode == 'auto') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65528) }}
          {% elif fan_mode == 'low') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65530) | bitwise_or(2) }}
          {% elif fan_mode == 'medium') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65529) | bitwise_or(1) }}
          {% else %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65531) | bitwise_or(3) }}
          {% endif %}

  set_swing_mode:
    - service: modbus.write_register
      data:
        hub: fancoil_hub
        slave: 2
        address: 201
        value: >
          {% if swing_mode == 'on') %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_or(128) }}
          {% else %}
          {{ states('sensor.fc_soggiorno_prg') | int | bitwise_and(65407) }}
          {% endif %}
litinoveweedle commented 6 months ago

Hello,

if you like, you can use my forked repo, where I am merging multiple feature request with some of my own patches. ;-) The above error is patched there.