Custom component to declare/set variables

Hello, give example plz

Like:
Data: {
“value” : “{{Buterbrod}}”
“variable”: “moi_zavtrak”
}

Why doesn’t this update my variable?

- alias: "Automation - Update"
  trigger:
    - platform: state
      entity_id: sensor.consumption
  action:
    - service: variable.set_variable
      data:
        variable: usage
        value_template: "{{ (trigger.to_state.state|float-trigger.from_state.state|float)|round(3) }}"

And changing the action to this does work:

  action:
    - service: variable.set_variable
      data:
        variable: usage
        value_template: "{{ (now().minute/1000)|round(3) }}"

@rogro82.
I need help please.
I am a newbie to Home Assistant so if emailing you for help is an inappropriate thing to do please accept my apologies.

I am trying to use your variables as data to a service call but I just am not getting the result I need.

I wonder if you could please advise what I’m doing wrong.

I have included two of the service calls in the scripts.yaml. The one in bold tries to use the variables to give the same result as the one not bold.

The one not bold works fine and an 8 is sent to my TV. The one with the variables does not work.

Configuration.yaml
var: !include variables.yaml

variables.yaml
which_remote:
  friendly_name: "which_remote"
  initial_value: remote.rmpro_remote
which_freesat:
  friendly_name: "which_freesat"
  initial_value: "lounge freesat"

automations.yaml
- alias: 'TV Channel BBC1'
  trigger:
    - platform: state
      entity_id: input_select.freesat_channel
      to: 'BBC1'
  action:
    - service: script.lounge_tv_channel_9
    - service: script.lounge_tv_channel_5
    - service: script.lounge_tv_channel_8

Scripts.yaml
lounge_tv_channel_8:
    alias: Lounge TV channel 8
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rmpro_remote
          device: lounge freesat
          command: b64:JgBgAAABJpMSExETEhISExETEhISExETEhMRExETEhMROBITERMSEhITETgTEhE4EhMRExISEhMROBITETgTEhE4EjgROBI4EwAGHwABJkoSAAxEAAEmSRQADEQAASVLEgANBQAAAAAAAAAA
lounge_tv_channel_9:
    alias: Lounge TV channel 9
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: var.which_remote
          device: var.which_device
          command: b64:JgBYAAABJpMTEhETEhISExISEhISExETEhMRExETEhMROBITERMTERI4ETgSExE4ExIRExITERMRExITETgTEhE4EzcSNxM3EwAGHwABJkoTAAxDAAEmShMADQU`

You need to set variable with variable.set_variable service call in automations. I don’t see that in your code. I had some problems with HA 0.117 and original rogro82 component, so i replaced it with forked version. https://github.com/Wibias/hass-variables Also are you sure that you need this custom component? What do you try to achieve with this automation?

Vladimir,

Thank you for your response. I didn’t try to set variables yet as I wanted to make sure everything would work using the initial value settings.

You ask what I am trying to do. Well I have two set top boxes in different rooms that are controlled by the same infrared codes. I have a Broadlink RMpro in one room and a Broadlink Mini in the other. I didn’t want to duplicate code to use the individual IR sender so my plan was to set the sender at the top of each automation.

My final thought was to set the IR code as well to minimise coding completely.

Perhaps I should try your fork?

Thanks again

I have had issue setting variables in automations, though I believe they work when set in a script - perhaps have the automation call a script?

I have not yet tried the new fork.

I’m also contemplating moving all these variables to input_* (as I was a noob when I first setup variables custom_component a year ago and wasn’t aware of them). I’m also still on .116.4, so can’t speak to if they work in .117.

Also check your entity name for variable. And check state of that variable with template editor

{{states.variable.VARIABLE_NAME.state}}

I entered what you suggested replacing the VARIABLE_NAME. Sorry to ask but where will I see the state? In the log?

My variables do show up on my overview page and I can see they have the right values.

Mark,
Thanks for you reply. I believe the variables are set via the initial value and I can see them on my Overview page and they appear correctly.
Please accept my apologies for asking but how do I call a script from an automation. I thought that was what I was doing when I called the service from the automation.

Go to Developer Tools > template editor

Yes I did that.

It shows me to the right “This template listens for the following state changed events:”

  • Entity : variable.which_remote

Where can I see the results of its listening?

I just noticed this error in the log.
Logger: homeassistant.helpers.service
Source: helpers/service.py:447
First occurred: 15:11:54 (1 occurrences)
Last logged: 15:11:54

Unable to find referenced entities var.which_remote

Maybe you should call entity_id in automation variable.which_remote and not var.which_remote?

I’m not sure that I’ve fully read all of the thread related to your issues but just be aware that there are two different custom components for variables.

One of them is the rogro82 component and the other is by snarky-snark: https://github.com/snarky-snark/home-assistant-variables.

the one by snarky-snark uses the “var.” domain. the one by rogro82 uses the “variable:” domain.

Thanks. OK I was not aware of that and had become confused by the two.

The one I’m attempting to use is the snarky-snark “var.” domain.

In trying to find solutions I obviously strayed over into the one by rogro82.

I’ll make sure my continuing search stays in the “var.” domain.

Hi all,

I use this component for have an history of movements like the example in the github.

I just updated to 0.118 and I see this error in log:

Update Last Motion: Error executing script. Invalid data for call_service at pos 1: template value should be a string for dictionary value @ data[‘attributes_template’]

It is related to the native template coming with 0.118? I tried to read docs but I’m not a programmer so I cannot understand how to fix it. Any help appreciated.

Thank you!

see this thread for the fix:

1 Like

Is this component dead? I use it and its not viable in 2021.6 anymore?

Use this fork.

1 Like