#Multiply/Add to !input

18 messages ยท Page 1 of 1 (latest)

willow basin
#

I'm trying to multiply by an !input number, but I keep getting various errors...
Here is my input...

  input:
    start_before:
      name: Minutes Before Alarm
      description: How many minutes before the alarm time to start the automation.
      selector:
        number:
          min: 1
          max: 60
      default: 30

And my action...

action:
  - action: light.turn_on
    data:
      color_temp_kelvin: !input start_kelvin_temp
      brightness_pct: 1
      entity_id: !input target_light
  - delay:
      seconds: 5
  - action: !input alarm_script
    data:
      target_kelvin: !input target_kelvin_1
      start_kelvin: !input start_kelvin_temp
      max_brightness_pct: !input max_brightness_1
      alarm_length: "{{ (start_before - 2) }}"
      steps_per_minute: !input steps_per_minute
      light_timeout: 0
      target_light: !input target_light

I just want to be able to multiply or subtract from the start_before value.

plain pebble
#

I think you need to assign input parameters to a variable before they can be used in a template. They're not automatically added to the template environment

willow basin
#

Hmm... Okay.
This is my first yaml automation, I'm really fumbling my way through.
I'll take another look at templates and input parameters.
I did try making a variable, but I'm never 100% sure if it's an issue with yaml or home assistant syntax.

willow basin
#

So, the variable will be set in the action?

paper hinge
willow basin
#

Awesome, thank you.
I was able to get it working with

variables:
  start_before: !input start_before

trigger:
  - platform: template
    value_template: "{{ now().timestamp() | timestamp_custom('%H:%M') == (state_attr('input_datetime.alarm_time', 'timestamp') - start_before) | timestamp_custom('%H:%M', false) }}"

condition:
  - condition: state
    entity_id: !input workday_sensor
    state: "on"

action:
  - action: light.turn_on
    data:
      color_temp_kelvin: !input start_kelvin_temp
      brightness_pct: 1
      entity_id: !input target_light
  - delay:
      seconds: 5
  - action: !input alarm_script
    data:
      target_kelvin: !input target_kelvin_1
      start_kelvin: !input start_kelvin_temp
      max_brightness_pct: !input max_brightness_1
      alarm_length: "{{ start_before * 0.5 }}"
      steps_per_minute: !input steps_per_minute
      light_timeout: 0
      target_light: !input target_light

I still feel like I'm hacking my way through everything though...
Like, I don't know when I'm supposed to use "{{}}" or just {{}}.

paper hinge
#
alarm_length: "{{ start_before * 0.5 }}"

Needs quotes because it is a bare template in the YAML. That ' " ' triggers Jinja engine to see it it needs to do something.

alarm_length: -
  {{ start_before * 0.5 }}

Does not need the quotes because the '-' triggers jinga for everything in that block

plain pebble
willow basin
#

Any docs for figuring out why the trigger isn't working?
I'm getting an error about an unsupported operand type for -: 'NoneType' and 'int'

#

Also, just in general, would you mind sharing your workflow?
I'm basically just writing the yaml and (trying to) run it in Home Assistant and relying on its logs. Is there a better way to debug?

paper hinge
plain pebble
willow basin
# plain pebble that sounds like `input_datetime.alarm_time` doesn't exist, or the attribute doe...

Pretty sure you're right.
alarm_time is created here

  input:
    alarm_time:
      name: Alarm Time
      description: Set the alarm time (when you want the light at full brightness).
      selector:
        entity:
          filter:
            - domain: input_datetime

So I think another variable is in order. input_datetime.sunrise_alarm does exist from that, but I don't see why I'd use that.

Thanks, guys!
With Home Assistant's own syntax and entities and helpers and templates, and I'm not super experienced with yaml, it's a lot at first!

willow basin
#

I just found the template editor in the Dev Tools ๐Ÿ˜

paper hinge
#

You are doing great. Asking the right questions.