Custom component to declare/set variables

Using the Developer Tools-> Services to call this:

script.google_home_notification
{"device":"media_player.craft_room_google"}

scripts.yaml

google_home_notification:
  sequence:
    - service: script.google_home_turn_on
      data_template:
        device: "{{ device }}"
    - service: variable.set_variable
      data:
        variable: last_volume
        value_template: "{{ state_attr( 'media_player.craft_room_google' ,'volume_level') }}"

Above code works fine, calls another script.google_home_turn_on using the correct passed ‘device’, but if I try to change ‘media_player.craft_room_google’ (like above) to device (shown below) [to make it dynamic], it fails.

        value_template: "{{ state_attr( device ,'volume_level')}}"

Kicks out this error in the logs:

2019-08-28 18:02:34 ERROR (MainThread) [custom_components.variable] Could not render value_template variable.last_volume: UndefinedError: ‘device’ is undefined

Assuming it’s something in the custom_component that’s not allowing the ‘passed’ device variable from being used. Trying to just use a single dynamic script if at all possible @rogro82 . Thx!

This question is asked multiple times in this topic, but never really answered it seems:

What is the advantage of using this component over using input_number or input_select?

I am using this component to restore previous state in chromecast. Like this:
Radio playing > something interrupted radio(tts) > save radio station and volume to variable history > tts stops > restore from variable history radio station volume to CC.

I think the biggest one is that you can also set attributes without the limitation of 255 characters.
For example, im setting the attributes of a variable from a csv file with a python shell script.


These attributes can then be used to show the csv content in the custom list card.

1 Like

Something like this:

1 Like

can you share your template sensor please?

was this addressed to me? if so, which template sensor are you referring to? My post was referring to an automation using the variable custom component.

@Mariusthvdb, yes sorry for not being clear

I like the screenshot you have posted above, I assumed this being created with a template sensor.
If its not the case, I still want to know how you achieved this to show the the history of the different motion sensor in 1 big sensor view.

a ok, sure, I use this automation I built using my motion sensors:

automation:
# Update Last Motion variable
  - alias: 'Update Last Motion'
    id: 'Update Last Motion'
#    initial_state: 'on'
    trigger:
      platform: state
      entity_id:
        - binary_sensor.laundry_motion_sensor
        - binary_sensor.dining_table_motion_sensor
        - etc etc etc
      to: 'on'
    action:
      service: variable.set_variable
      data:
        variable: last_motion
        attributes_template: >
          {
                "history_1": "{{ variable.state }}",
                "history_2": "{{ variable.attributes.history_1 }}",
                "history_3": "{{ variable.attributes.history_2 }}",
                "history_4": "{{ variable.attributes.history_3 }}",
                "history_5": "{{ variable.attributes.history_4 }}",
                "history_6": "{{ variable.attributes.history_5 }}",
                "history_7": "{{ variable.attributes.history_6 }}",
                "history_8": "{{ variable.attributes.history_7 }}",
                "history_9": "{{ variable.attributes.history_8 }}",
                "history_10": "{{ variable.attributes.history_9 }}"
          }
      data_template:
        value: >
          {{ trigger.to_state.attributes.friendly_name|replace('motion sensor','') }}:
          {{as_timestamp(states.variable.last_motion.last_changed)| timestamp_custom('%X') }}

and variable:

##########################################################################################
# Inputs
# https://diyfuturism.com/index.php/2017/12/15/useful-sensor-motion-last-seen-________/
##########################################################################################

variable:
  last_motion:
    value: 'Not set'
    restore: true
    attributes:
      icon: mdi:map-marker
      friendly_name: 'Last Motion'

in the link in my comments, there’s some extra info on this custom component

1 Like

I think the biggest one is that you can also set attributes without the limitation of 255 characters.

Attributes don’t have a 255 character limit, only states do.

As i said.

Can this (or similar) component solve the challange in this post? Namely I would like to control entity templates in one place and re-use them where needed. Thanks!

Is there a way to delete attributes from the variable entity after you create them?

I updated the attribute names in a varaiable. set_variable service call and instead of changing the existing variable attributes it just added the new ones to the list. Now I have a bunch of unused attributes and the list is twice as long.

please help, I am using the variable and setting a value just find
when i try and use the variable in a template , the value is not resolving.

data_template:
event: send-sam-reminder
value1: {{ variable.messagetosend }}
service: ifttt.trigger

reposting with correct indentation

data_template:
   event: send-sam-reminder
   value1: {{ variable.messagetosend }}
service: ifttt.trigger

never mind if fixed it

I use them like this:

{{ states.variable.messagetosend.state }}

Hello. I’ve tried to update/change a variables value and/or its attributes in node red with call service nod, but it doesn’t work. Node configuration:
Server: Home Assistant
Domain: variable
Service: set_variable
Entity Id: variable.example_123
Data: { “value” : “{{Buterbrod}}”}
And… Debug: “Call-service API error. Error Message: extra keys not allowed @ data[‘entity_id’]”
What should I do to make it work?

Do not use Entity_id field. Use this part in data field.

Hello, give example plz