Divided by 2 - input_number

Hello,

I want to half time of the input_number state. If I use - 7, it works, but not /2

How I want it:

      - platform: state
        entity_id: switch.bewasserung_tomaten
        to: "on"
        for:
          minutes: "{{ (states('input_number.bewaesserung_tomaten')|int) / 2 }}"

How it works:

  - alias: "Bewässerung Tomaten kurz"
    trigger:
      - platform: state
        entity_id: switch.bewasserung_tomaten
        to: "on"
        for:
          minutes: "{{ (states('input_number.bewaesserung_tomaten')|int) - 7 }}"
    condition:
      - condition: or
        conditions:
          - condition: state
            entity_id: sensor.rain_status_2_a
            state: 'Regen'
          - condition: state
            entity_id: sensor.rain_status_1_a
            state: 'Regen'
    action:
      - service_template: switch.turn_off
        entity_id: switch.bewasserung_tomaten

What is wrong??

Just a guess, but division sometimes results in a float type cast. Maybe just moving the |int after the /2 will work?

          minutes: "{{ ((states('input_number.bewaesserung_tomaten')) / 2) |int }}"

you can’t do that. States are always strings so you have to convert the state to a number before you can do math on it.

However to the OP…

I’m not sure why you can’t get it to work. I just tried it in the template editor and it works fine:

ex3

1 Like

Ah, so this sounds like a double-whammy… gotta convert the string before, and the float after. Maybe this:

          minutes: "{{ ((states('input_number.bewaesserung_tomaten')|int ) / 2) |int }}"

I’m guessing minutes: doesn’t like the .5? IDK since I use nodered for automations.

I also guess this is the problem.

@Griesi
Does it need to be exactly half of the input number or would rounding down be an option as well? Another question, why do you not set the input number to the number of minutes you want instead of twice the number? Are you using this for something else as well?

No choice, the OP will have to round up or down if minutes: only accepts ints. LOL, if just using the correct input number is doable. :wink:

Yep if he looks in the log there would have been an error advising an int was required. I had exactly the same problem a couple of days ago.

He does have a choice, he shoudl be able to make a floor division for the minutes and convert the remainder to seconds and put them in seconds.

1 Like

I like this… simplest way to skin this cat is just dealing with seconds internally.

Now, that I think about it a bit more, is it possible to have something in seconds above 60? Then it would be as simple as multiplying “minutes / 2” by 60 and put that into seconds.

Excellent question… just scanned over the docs and nothing conclusive there. Seems a range outside of 0-59 would work, but if not then that should be in the docs.

Minutes div 2 times 60

You mean mins * 30.???
:rofl:

Yes this is doable

I’ve used mins upto 720, no problem

But, why doesn’t he just use step 2 on the input number as an alternate

1 Like

No that’s too easy :rofl: :rofl: :rofl: :rofl: :rofl:

Rounding is OK But I als tryed only even numbers…

Try this:

- platform: state
  entity_id: switch.bewasserung_tomaten
  to: "on"
  for:
    seconds: "{{ ((states('input_number.bewaesserung_tomaten') | int) * 30 ) | int}}"
1 Like

Yep, but my comment above was mostly in jest as if I see
X | int * 60 / 2
Then I will have a clue that its half the minutes, used as seconds

They both work and I don’t really have a problem with either. :smiley:

1 Like