Set variables, custom component variables

I am using the custom compenent varaiables
https://github.com/rogro82/hass-variables

I would like to use a variable and set it to the value of an input_number, but got stuck.

configuration.yaml

    variable:
      hour_calc:
        value: 0
        restore: true
      min_calc:
        value: 0
        restore: true

automations.yaml

- alias: slider_start_time
  hide_entity: false
  trigger:
    platform: state
    entity_id: input_number.start_hour
  action:
    - service: variable.set_variable:
      data:
        variable: hour_calc
        value: input_number.start_hour

This does not work:
Configuration invalid
Error loading /home/homeassistant/.homeassistant/configuration.yaml: mapping values are not allowed here
in ā€œ/home/homeassistant/.homeassistant/automations.yamlā€, line 75, column 37

I have tried to understand the value_template and data_template but still no luck ā€¦ Anyone that knows how to store an input: number into another variable

Next question is how to set a variable as the value of itself multiplied with 3600.

action:
    - service: variable.set_variable:
      data:
        variable: hour_calc
        value: hour_calc * 3600
1 Like

Remove the colon at the end of the service call.

There are examples on github

1 Like

Thank you, easy one that I missed the ā€œ:ā€ =)

Still have no luck in setting the variable even with the example. Tried this one:

- alias: slider_start_time
  hide_entity: false
  trigger:
    platform: state
    entity_id: input_number.start_hour
  action:
    - service: variable.set_variable
      data:
        variable: hour_calc
        value_template: '{{ (variable.state = input_number.start_hour | int) }}'

With this error message:
2018-07-07 14:00:58 ERROR (MainThread) [homeassistant.core] Invalid service data for variable.set_variable: invalid template (TemplateSyntaxError: expected token ā€˜)ā€™, got ā€˜=ā€™) for dictionary value @ data[ā€˜value_templateā€™]. Got ā€˜{{ ((variable.state | int) = (input_number.start_hour | int)) }}ā€™

value_template: "{{ states('input_number.start_hour') | int }}"

1 Like

Thank you so much!

1 Like

I have yet another question. I am trying to set an input_datetime time attribute to a value of a variable, but I cannot succeed. This is my last try:

- alias: slider_start_time
  hide_entity: false
  trigger:
    platform: state
    entity_id: input_number.start_hour
  action:
    - service: variable.set_variable
      data:
        variable: hour_calc
        value_template: "{{ states('input_number.start_hour') | int  * 3600 }}"
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.start_time
        date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
        time: timestamp.variable.hour_calc

And this is the error message from the log:
2018-07-08 22:24:42 ERROR (MainThread) [homeassistant.core] Invalid service data for input_datetime.set_datetime: Invalid time specified: timestamp.variable.hour_calc for dictionary value @ data[ā€˜timeā€™]. Got ā€˜timestamp.variable.hour_calcā€™

Any suggestions?

        date: "{{ now().date() }}"
        time: "{{ states('variable.hour_calc')|int|timestamp_custom('%H:%M:%S',False) }}"

I donā€™t know if youā€™re using variable.hour_calc elsewhere. But if not, then you could do this:

        date: "{{ now().date() }}"
        time: "{{ (states('input_number.start_hour')|int*3600)|timestamp_custom('%H:%M:%S',False) }}"

Or even:

        date: "{{ now().date() }}"
        time: "{{ states('input_number.start_hour') }}:00:00"
1 Like

Great, thanks again!

1 Like

Hi allā€¦ Iā€™m having a bit of a problem setting these variables. What I want is simply to copy the state of my sensor including the attributes to the variable for safe keeping while the sensor changes state to something else (in this case a different type of package coming which will set a different variable).

I canā€™t get it to set the variable at all right now and Iā€™m not sure why. Iā€™ve used very similar triggers and conditions before, so I think Iā€™m doing something wrong in the syntax to set the variable. Any ideas?

- id: posta_state_change
  alias: 'Posta state change'
  trigger:
    platform: state
    entity_id: sensor.cp_packages_coming_today
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: 'sensor.cp_packages_coming_today'
        state: 'delivery'
      - condition: state
        entity_id: 'sensor.cp_packages_coming_today'
        state: 'no_delivery'
  action:
    service: variable.set_variable
    data:
      variable: posta_variable
      value_template: "{{ states('sensor.cp_packages_coming_today')}}"
      attributes_template: >
        {
          "from": "{{ state_attr('sensor.cp_packages_coming_today', 'from') }}",
          "date": "{{ state_attr('sensor.cp_packages_coming_today', 'date') }}",
          "subject": "{{ state_attr('sensor.cp_packages_coming_today', 'subject') }}",
          "body": "{{ state_attr('sensor.cp_packages_coming_today', 'body') }}"
        } 

One more thing is that the icon isnā€™t setting correctly. icon: appears as an attribute but the actual icon doesnā€™t change.

variable:
  posta_variable:
    attributes:
      icon: mdi:delivery
      friendly_name: 'Posta'
      from: ''
      subject: ''
      body: ''
      date: ''
    restore: true

Hello scstraus

Did you ever solve this ? I have excatly the same issue. It doesnā€™t pupulate the attribute part.

If I comment out the attribute part, I can set the variable value, without problem.
It doesnt throw any errors in the logs.

/Michael

No, unfortunately not. Iā€™ve run out of ideas. I feel like it should work one of the ways Iā€™ve tried but it simply doesnā€™t.

Have you tried to delete the icon attribute?
Maybe this causes errors
I always set the icon and friendly_name via customize.yaml
That works well

If you all are still having trouble with that variable component, you might want to check mine out to see if it suits your needs.

It doesnā€™t support an attributes dictionary, but itā€™s easy enough to just create a variable for each attribute. Itā€™s also possible to create a dict in a jinja template and store that in a variable instead.

2 Likes

Hello Snarkysnark

This worked like a charm ! Thanks.

Michael

1 Like

Allright. I hope this reply is in the right topic. If not, Iā€™m sorry.

I wonder if the following is possible within HA, because it would make things a lot easier. Lets say:

@ 6AM I want my bedroo lights to turn on @ brightness 5 and every 3 minutes I want them to get 5+ brightness. What I do now is enter a wait of 00:03 and add a new brightless lever. Bit can this be done easier?

The same is for example my sonos
If I want the sonos to start playing at volume 0.02 @ 6.10 and get 0.01 louder every 5 minutes, I now have to add a wait / etc. Isnā€™t it pissible to add some kind of tempate to add X per X minutes?

Iā€™m not at home right now so I canā€™t look into further at the moment. But somewhere on here thereā€™s a script that I found that does exactly that. You tell it the start time, how long of a ramp you want and from what level to start and finish. It does ramp down as well. I use for my wife and I everyday. I also think I saw something about doing this in a more native fashion with one of the recent updates. But what I have works so I didnā€™t pursue it.

Hey, I tried no all kinds of options to save a variable and get it out but nothing is working not the snarky one there I manage to get variables in but I canā€™t use them in code. input number I just not get it I cant set a sensor value and store ist there. and then there are the Hass variables the same thing itā€™s a nightmare.
I just want to store a sensor value and subtract it the next day to get the difference why is that so difficult.
Example: I have a sensor with a value of 50 today tomorrow it is 53 maybe and I want to preserve the 50 and subtract the 50 from the 53 to get the difference.
it really canā€™t be that hard but I get it with no variable solution to work.

anyone can help me I just donā€™t understand the examples of why I make a query and have events if I just want the var.x back and why canā€™t I just use x.var and thatā€™s it?

I think this topic is outdated. using entities of type input_boolean etc. solved the issue for me

will this help ?

Not outdated at all.

Iā€™m on v116.2 and hass-variables are still working fine for me.