Custom Component: Generic Variable Entities

That I agree with that. There doesn’t seem to be much activity on the repo dealing with issues.

@snarkysnark, congrats on this Component. I’m trying to use it to do something probably quite simple but am struggling.

I want to use it to store a relatively complex message to be spoken out of Alexa’s and Google Homes using TTS.
I COULD just build these messages in the Automations that trigger the TTS but, as I have a mixture of devices I wanted to build the message once and then call it for each device.

I have dummed down my config to save on space. I think I can set the variable correctly in configuration.yaml thus:

var:
  alprmessage:
    initial_value: 'Test message.'

But when I call it in my automation thus, it doesn’t work:

- alias: Webhook Test
  trigger:
    platform: webhook
    webhook_id: openalpr
  action:
    entity_id: media_player.living_room_speaker
    data_template:
      message: '{{var.alprmessage}}'
    service: tts.google_say

As you can see it’s triggered by the receiving of a webhook (data from which will eventually form part of the message)… and this works fine to trigger the automation.

Can you spot where I’m going wrong? There’s lots of instruction around on how to set and update variables as well as how to display the data on Cards etc. but I can’t see anything on how to use the data in automations etc. Any assistance gratefully received. :slight_smile:

A var entity is just like any other HA entity in this regard.

{{ states('var.alprmessage') }} 

Thank you very much for this snarkysnark however it is still not working with my configuration. I suspect I’m an outside-case here though… using notifications etc. It’ll probably be due to positioning of quotes (’) and such-like. I have played around but still not got anywhere. I won’t give up just yet though. :slight_smile:

"{{ states('var.alprmessage') }}"

Is it possible to select the content of a database table into a variable. I want to read each record into array of attributes, like in case of the forecast data of the weather entity:

temperature: 17.8
humidity: 94
ozone: 309.5
pressure: 1014.1
wind_bearing: 208
wind_speed: 3.27
visibility: 16.093
attribution: Powered by Dark Sky
forecast: 
- datetime: '2020-06-30T22:00:00+00:00'
  temperature: 28.9
  templow: 17.9
  precipitation: 0.1
  wind_speed: 6.85
  wind_bearing: 202
  condition: partlycloudy
- datetime: '2020-07-01T22:00:00+00:00'
  temperature: 29.9
  templow: 18.8
  precipitation: 4
  wind_speed: 4.53
  wind_bearing: 78
  condition: rainy

This is not currently possible, but it looks like it might be pretty simple to implement. I would encourage you to post a ticket on the issue tracker on the project GitHub page, or even better, a pull request!

It looks like this line is all that would need to change.

I think this change would do the trick

return res[self._column] if self._column is not None else res

That said, I’m not completely sure what will be returned if the number of records returned by the query is not exactly 1. In fact, I’m not sure what is returned in that case even when the column is present. Is it an array?

I have already done it: https://github.com/snarky-snark/home-assistant-variables/issues/33

@snarkysnark, thanks for the integration/component. I’ve only come across it and it looks good for adding extra options.

Rather than a counter, I would like to store the last time a door/window contact was opened, lets say binary_sensor.garage_gate and the value/time subsequently survive reboot, etc.

What would be the best way of doing that using your integration?

There are multiple ways to achieve this. One way would be to listen to the event generated by your door sensor. Another way (shown below) is to monitor updates to your binary sensor’s state and update the variable based on that state.

This is untested, but should give you a general idea.

var:
  door_time_last_opened:
    friendly_name: "Last Time Door Was Opened"
    value_template: >-
      {% if states('binary_sensor.garage_gate') == true %}
        {{ as_timestamp(now()) }} 
      {% else %}
        {{ states('var.door_time_last_opened') | int }}
      {% endif %}
    tracked_entity_id:
      - binary_sensor.garage_gate

You may also want to check out this documentation to learn more about handling time in jinja2 templates: https://www.home-assistant.io/docs/configuration/templating/

Edit:

Instead of as_timestamp(now)) you might also try states.binary_sensor.garage_gate.last_updated

2 Likes

Thanks for the info & quick reply @snarkysnark, it is much appreciated. I will look how I can adapt to my contact & motion sensors.

I’m looking for help because I can’t get this component working unfortunately. I have the following include for the component

var: !include variables.yaml

and then the variables.yaml

manga_tracker_release_api:
  friendly_name: "Manga tracker release api"
  initial_value: "unavailable"
  icon_template: >-
    {% if states("var.manga_tracker_release_api") == "available" %}
      mdi:api
    {% elif ("var.manga_tracker_release_api") == "unavailable" %}
      mdi:api-off
    {% endif %}

When I commit this change, the config check fails with the message

expected dict for dictionary value @ data[‘packages’][‘manga_tracker_release_api’][‘friendly_name’]

I went by the docs, so what am I doing wrong here?

This is unexpected. What happens if you comment out the icon template? Does the config check still fail?

The other thing to try would be to inline the var config instead of using !include. It might be an issue with how includes is being used.

1 Like

Just tried it out. That one didn’t change anything

The other thing to try would be to inline the var config instead of using !include . It might be an issue with how includes is being used.

But THAT made the difference. Now it works with my initial setup. I really want to put this stuff in another file though. Any idea how to make that work? Surely it must work somehow

It’s likely a syntax issue. Are the variables in variables.yaml each prefixed with a -?

When I have it like

- manga_tracker_release_api:
    friendly_name: "Manga tracker release api"
    initial_value: "unavailable"
    icon_template: >-
      {% if states("var.manga_tracker_release_api") == "available" %}
        mdi:api
      {% elif ("var.manga_tracker_release_api") == "unavailable" %}
        mdi:api-off
      {% endif %}

- manga_tracker_userdata_api:
    friendly_name: "Manga tracker userdata api"
    initial_value: "unavailable"
    icon_template: >-
      {% if states("var.manga_tracker_userdata_api") == "available" %}
        mdi:api
      {% elif ("var.manga_tracker_userdata_api") == "unavailable" %}
        mdi:api-off
      {% endif %}

i get the error message

Package general_package setup failed. Component var cannot be merged. Expected a dict.

Also another question I have regarding the component: Can I add custom attributes to the variable and change them via service? If yes, how would I do that? I haven’t found anything about attributes in the docs

Maybe change the configuration.yaml, I’ve setup mine like:
var: !include_dir_merge_named configs/var/

And then in the var.yaml don’t uses ‘-’ before the var declaration.

It’s possible, with a bit of a workaround.

1 Like

Hello, Having an issue with SQL query and hoping someone can help. I’m using Home Assistant OS as a virtual machine with the MariaDB Add-on. I’ve verified recorder is setup properly and is storing status updates and events to the homeassistant database in MariaDB. I’ve verified the SQL query is good by using PHPmyAdmin to run the query. I’ve reviewed the logs and there are no errors. Using the latest version of Core (2021.2.3), Supervisor (2021.2.11) and Host (Home Assistant OS 5.12). Running version 0.10.0 of home-assistant-variables installed via HACS. Followed the direction closely and not sure what the issue could be.

Goal is to perform the query every minute and update the variable but the variable status remains unknown and does not appear to be tracking entity id.

Does this component follow the custom db_url for recorder?

Anyone have an idea?

Below is the var sensor config

front_porch_max_temp:
  friendly_name: "Front Porch Max Temp"
  icon: mdi:temperature-fahrenheit
  query: "SELECT MAX(state) AS max_value FROM states WHERE created > DATE_ADD(CURRENT_DATE, INTERVAL +8 HOUR) AND entity_id = 'sensor.porch_thgr810_temperature';"
  column: 'max_value'
  tracked_entity_id: 'sensor.time'

Screen Shot 2021-03-02 at 3.40.49 PM