Setting variables in an automation

Tags: #<Tag:0x00007f325b2ab6a8> #<Tag:0x00007f325b2ab568>

My goal is to get a value from a sensor (temperature) and do some math with it and add this new value to a variable.
Afterwards I like to get the value of this variable back in an automation.

I am confused with the use of variables:

  1. how do I have to initialize a variable in automation.yaml? Is this an extra service?
  2. how can I update the variable with a new value?
  3. how can I use this variable in the action of the automation?

My example is this one:

- id: '1613935335031'
  alias: Temperature to RGB
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.unk_manufacturer_unk_model_b2bd6c22_temperature
  condition: []

  action:
  - service: variable.set_variable
    data:
      variable: 
    
  - service: light.turn_on
    entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
    data:
      brightness: <variable here>
      rgb_color:
      - 0
      - 0
      - 255
  mode: single

I like to set the temperature in a variable, do some calculations and then update the variable with the new calculated value.

Is this a good entry point for my questions?

To prevent confusing new users, this:

service: variable.set_variable

Is not part of Home Assistant’s variables. That service call belongs to a custom integration that implements its own style of variables (namely the one created by user snarky-snark).

1 Like

I am a new users and I think this is what confused me also :slight_smile: thanks for the clarification

Perhaps a Template Sensor can serve that purpose.

Your automation would simply refer to the Template Sensor’s value.

What is the calculation that want to compute?

1 Like

I don’t know yet how to calculate but the goal is to increase the Red value of RGB if it gets warmer and decrease if it gets colder.
At the same time the blue value should increase if it gets colder.

The RGB light takes values from 0 to 255.

You will need to determine the temperature range. For example, rgb_color is solid red [255, 0, 0] when the temperature is 25 C or more and solid blue [0, 0, 255] when the temperature is 0 C or less. That represents a range of 25 degrees.

The midway point is approximately 12 degrees and that’s where red and blue are half their max values: [127, 0, 127]. For every 1 degree increase, the red value should increase by 255/25 ~ 10 and the blue value should decrease by the same amount (10).

That’s one possible algorithm for the temperature-based color effect you want.

1 Like

Ok thanks.

This looks also like an idea but my main problem is the understanding of the syntax.

# Example configuration.yaml entry
input_number:
  slider1:
    name: Slider
    initial: 30
    min: -20
    max: 35
    step: 1

If I want to start with an input_number do I have to declare them in the configuration.yaml? From this example is the “entity_id” slider1 in this case?

I also found the service:
input_number.set_value

Should this be used as an action in the automation to do the calculation?

Sorry, I don’t understand the purpose of an input_number to compute the rgb value.

Is it to serve as a temporary replacement for the temperature sensor so that you can easily experiment with the calculation? If that’s the case then you can define an input_number in configuration.yaml (as per the example you posted) or via the UI using Configuration > Helpers.