vinteo / hass-opensprinkler

OpenSprinkler Integration for Home Assistant
MIT License
204 stars 40 forks source link

Run service in template switch doesn't work #284

Closed cjhesse closed 3 months ago

cjhesse commented 4 months ago

I'm not certain whether this is an issue with this integration, HASS, or my templating. That being said, the exact same service YAML works using the developer tools.

Here's what I have in configuration.yaml:

switch:
  - platform: template
    switches:
      sprinkler:
        friendly_name: Sprinkler
        value_template: >-
          {{ is_state('binary_sensor.z1_left_side_station_running', 'on') or
             is_state('binary_sensor.z2_back_main_station_running', 'on') or
             is_state('binary_sensor.z3_back_corner_station_running', 'on') or
             is_state('binary_sensor.z4_right_side_station_running', 'on') or
             is_state('binary_sensor.z5_front_garden_station_running', 'on') or
             is_state('binary_sensor.z6_front_main_station_running', 'on') }}
        availability_template: >-
          {{ has_value('binary_sensor.z1_left_side_station_running', 'on') or
             has_value('binary_sensor.z2_back_main_station_running', 'on') or
             has_value('binary_sensor.z3_back_corner_station_running', 'on') or
             has_value('binary_sensor.z4_right_side_station_running', 'on') or
             has_value('binary_sensor.z5_front_garden_station_running', 'on') or
             has_value('binary_sensor.z6_front_main_station_running', 'on') }}
        turn_on:
          service: opensprinkler.run
          data:
            entity_id: switch.opensprinkler_enabled
            run_seconds:
              0: 60
              1: 60
              2: 60
              3: 60
              4: 60
              5: 60
            continue_running_stations: true
        turn_off:
          service: opensprinkler.stop
          data:
            entity_id: switch.opensprinkler_enabled

When turning the switch on, nothing (apparently) happens. I didn't see any warnings or errors in the log that appeared related.

Turning the switch off does successfully stop the sprinkler when it's already running.

I tested the same "turn on" action in Developer tools > Services, and that worked:

service: opensprinkler.run
data:
  entity_id: switch.opensprinkler_enabled
  run_seconds:
    0: 60
    1: 60
    2: 60
    3: 60
    4: 60
    5: 60
  continue_running_stations: true
EdLeckert commented 3 months ago

The only problem I see with your template is that has_value only takes one parameter, but that's not relevant to the failure.

When run_seconds is passed via Developer tools > Services, the keys are converted to strings, whereas when passed via the template, they are passed as integers. Since the code is expecting strings, it fails, but quietly so that all run durations are 0.

I suspect this worked in the past since the examples are written using integers, but it no longer works. However, the fix is easy--simply pass the keys as strings:

            run_seconds:
              '0': 60
              '1': 60
              '2': 60
              '3': 60
              '4': 60
              '5': 60

I will be submitting a fix to the code so that either will work.

cjhesse commented 3 months ago

The only problem I see with your template is that has_value only takes one parameter, but that's not relevant to the failure.

That's what I get for using copy/paste...

I will be submitting a fix to the code so that either will work.

Tested the new update and my template indeed works as-is (without converting the keys to chars).

Thanks!