Set variables, custom component variables

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.

I tried some things because i encountered the same problem but i got it fixed setting the variable attributes. I must say im pretty new to this type of coding and find it hard to get used to so my approach is mostly the trial and error method :laughing:

service: variable.set_variable
data:
  variable: rain_total_24h
  attributes: |-
    {
      "history_1": "{{ states('variable.rain_total_24h') }}",
      "history_2": "{{ state_attr('variable.rain_total_24h', 'history_1') }}",
      "history_3": "{{ state_attr('variable.rain_total_24h', 'history_2') }}"
    }

i have a problem with set the variable a (i don’t write anything in configuration.yaml i have write all code inside only in automation.yaml) with this my automation to find increase temperature

can you help me ? i don’ t know how i can resolve …

- alias: Controllo_incremento_temperatura301
  trigger:
  - platform: state
    entity_id: switch.sonoff_XXXX
    from: "off"
    to: "on"
  action:
  - variables:
      start_temperature301: "{{ states('sensor.sonoff_XXXX_temperature') }}"
      #counter: 0
  - alias: repeat until 
    repeat:
      sequence:
        - delay: 00:00:03
        - variables:
            new_temperature301: "{{ states('sensor.sonoff_XXXX_temperature') }}"
        - choose:
          - conditions:
              - condition: template
                value_template: "{{ (float(start_temperature301) + 0.1) < float(new_temperature301) }}"
            sequence:
              - variables:
                  a: 2   # this don't work
              - service: notify.mail
                data:
                  title: "Controllo Istantaneo temperatura 301"
                  message: "Incremento di 0.1 gradi rilevato"
          default:
            - variables: 
                a: 1  # this don't work
      until:
        condition:
          - or:
            - condition: template
              value_template: "{{ a != 2 }}"
            - condition: template
              value_template: "{{ repeat.index > 20 }}"
  - service: notify.mail
    data:
      title: "STATO CALDAIA ENTRO 15 MIN DA ACCENSIONE CALDO 301"
      message: >
        {% if a == 1 %}
          ALERT_CALDAIA NON FUNZIONANTE ENTRO 15 MIN_DOPO 301 CALDO ON
        {% elif a == 2 %}
          OK_CALDAIA FUNZIONANTE ENTRO 15 MIN_DOPO 301 CALDO ON
        {% else %}
          PROBLEMA NEL CODICE
        {% endif %}

how can i set variable a to 1 or 2
a=1 or a=2 ???
is the only tings that don’t work in this automation …

Good afternoon, I am trying to put an automation script together based off a Schedule. The challenge I have is setting the value of HVAC (Off or Heat) from a Scheduler (Off or On) - sorry for the ramble…

Schedule sends ‘on’ or ‘off’ based time, this works as I can include the state in a notification.

The switch takes hvac_mode: ‘off’ or ‘heat’

In my automation I need to map ‘on’ from the schedule to ‘heat’ and then use this variable in hvac_mode: %variable%

alias: Boiler Control - Weekdays PM
description: Boiler Control - Weekdays PM
trigger:
  - platform: state
    entity_id:
      - schedule.boiler_weekday_pm
    attribute: next_event
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state in ['on', 'off'] }}"
action:
  - service: climate.set_hvac_mode
    metadata: {}
    data:
      **hvac_mode:** heat # needs to be a variable set by "trigger.to_state.state"
    target:
      entity_id: climate.controller_boiler
  - service: notify.mobile_app_carey_work_phone
    metadata: {}
    data:
      message: "\"Boiler switched {{ states('switch.controller_boiler') }}\""
mode: single