button-card icon indicating copy to clipboard operation
button-card copied to clipboard

Math functions not getting expected sensor states in call-service

Open LastElf opened this issue 2 years ago • 5 comments

Checklist

  • [x] I updated the card to the latest version available
  • [x] I cleared the cache of my browser

Describe the bug I'm trying to set up an automatic shutoff for my AC in increments, and my AC system handles this via a service call with a minutes parameter (Though I may want a helper variable because of system delays calling this, at least from what I'm seeing the dev tools). The service expects a minutes value between 0-720 and I'm using HA to increment up/down in 15 minute chunks using math.max/math.min to make sure I don't call out of bounds.

The -15 button works exactly as expected, it goes down in 15 minute increments and stops at 0. The +15 button goes from 0 to 15 on the first tap, then the second one goes from 15 to 720.

Version of the card Version: 3.4.2

To Reproduce This is the configuration I used:

    - type: custom:button-card
      entity: sensor.ac_time_to_off
      show_icon: false
      name: '-15'
      tap_action:
        action: call-service
        service: advantage_air.set_time_to
        service_data:
          entity_id: entity
          minutes: >-
            [[[ return Math.max(states['sensor.ac_time_to_off'].state - 15,0)
            ]]]
    - type: custom:button-card
      entity: sensor.ac_time_to_off
      show_icon: false
      name: '+15'
      tap_action:
        action: call-service
        service: advantage_air.set_time_to
        service_data:
          entity_id: entity
          minutes: >-
            [[[ return Math.min(states['sensor.ac_time_to_off'].state + 15,720)
            ]]]

Expected behavior It's meant to increment up by 15 each time.

Desktop (please complete the following information):

  • Browser Brave
  • Version 1.35.103 (Chromium 98.0.4758.102) 64bit

LastElf avatar Feb 20 '22 05:02 LastElf

I know this is quite old but in case you didn't figure it out... Try to do Number(states['sensor.ac_time_to_off'].state). I'm pretty sure the state is stored as a string.

RomRider avatar Jul 26 '23 21:07 RomRider

I haven't tried again since I posted this. It's still happening in HA 2023.5 with whatever the latest supported Card-Mod is.

Just tested and confirmed that adding Number() breaks the service call.

LastElf avatar Jul 29 '23 02:07 LastElf

Can you show me the updated template wirh Number() and the state of the entity from the developer tools, please?

RomRider avatar Jul 29 '23 06:07 RomRider

Doesn't work:

      tap_action:
        action: call-service
        service: advantage_air.set_time_to
        service_data:
          entity_id: entity
          minutes: >-
            [[[ return Math.min(Number(states['sensor.ac_time_to_off'].state) + 15,720)
            ]]]

Works for the first but jumps from 15 to 720 on the second tap:

      tap_action:
        action: call-service
        service: advantage_air.set_time_to
        service_data:
          entity_id: entity
          minutes: >-
            [[[ return Math.min(states['sensor.ac_time_to_off'].state + 15,720)
            ]]]

image

LastElf avatar Jul 29 '23 07:07 LastElf

Sorry, I take that back, when I loaded up the modified to get the error output again it's now resolving the type casting properly. I must have fat fingered it the first time that cause the error.

Funnily enough, now it's iterating up in 15 minute increments properly, just to rub it in I'm sure. Still can't work out why the reducing button works without the strong typing but the increasing didn't. Guess I just need one of those "Don't touch this for fear of the gremlin" comments to remind me why I had to do something weird for a single case.

LastElf avatar Jul 29 '23 07:07 LastElf