HASS.Agent: Windows client to receive notifications, use commands, sensors, quick actions and more!

Hey all,

Been messing around today, and I’ve converted the RAM Usage of the HASS Agent from Percent into Usage/Total.

Now, most people probably don’t need this, but I find it easier to visualize a number rather than a percentage, so I though I’d share in-case anyone wanted it. It does require the use of a WMI Sensor and a template sensor to achieve, at least, for me it did.

HASS Agent WMI Query Sensor:

 WMI Query: SELECT Capacity FROM Win32_PhysicalMemory
    Scope: \\.\Root\CIMV2

I called laptop_ramtotal, but obviously, feel free to call it what you wish.

Home Assistant Template Sensor:

 - sensor:
        - name: "RAM Usage"
          state: >
            {% set ram = states('sensor.laptop_memoryusage') | float %}
            {% set ramTotal = states('sensor.laptop_ramtotal') | int * 2 / 1024 / 1024 / 1024 %}
            {% set usage = ramTotal | float / 100 * ram | round(0, default=0) %}
            {% set state = usage | string + "GB" + "/" + ramTotal | string + "GB" %}
    
            {{state}}

The sensor makes the assumption that you have 2 identical sticks of RAM installed, which you should have for performance, but if you only have 1 stick, simply remove the * 2.

sensor.laptop_memoryusage is my percentage sensor reported by HASS Agent by default. I simply changed my entity in the card from the HASS Percent Entity to my own sensor.

4 Likes

Thanks for the information. Useful. The more different examples there are with different implementations, the better it will be. Even if one of you finds it useless, it can be useful to the other. I’ll give you a :+1:. If there are other implementations, share :smiley:

I found an interesting feature in the work of Hass Agent. It’s about running the program through the Hass Agent. How do I run the program correctly?

If I put a check mark on run as ‘low integrity’, I can start the hyperion program by clicking on the button, but the hyperion program will start without displaying an icon in the Windows tray, and if without a check mark on run as ‘low integrity’, then clicking on the button, the hyperion program will not start.

This is what the button looks like, which, when pressed, launches the program

This is what the tray icon looks like when you just run the program, and if we run it with run as ‘low integrity’, then there will be no such icon in the tray
image

To run the program without the check mark on run as ‘low integrity’ and so that the program icon in the Windows tray is visible, I run the program through service: mqtt.publish in this way

You can find out the topic here

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: hyperiond

Here’s the most interesting thing, you can specify anything in the payload and hyperion will start anyway. Any of the options below will work and the hyperion program will run

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: C:\Program Files\Hyperion\bin\hyperiond.exe

---------------------------------------------------------------------------------------
service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: hyperiond

---------------------------------------------------------------------------------------
service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: ' '

Now the following. If you do not specify anything in the command field and leave the field empty, then you can run any program through service: mqtt.publish, though not any program. I use Windows 11 x64.

The cmd console doesn’t start for some reason

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/LIVINGROOM_launching_program/action
  payload: '"C:\Windows\System32\cmd.exe'"

And notepad starts

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/LIVINGROOM_launching_program/action
  payload: '"C:\Program Files\Notepad++\notepad++.exe"'
Текст на русском (Text in Russian)

Обнаружил интересную особенность в работе Hass Agent. Речь про запуск программы через Hass Agent. Как вообще правильно запускать программу?

Если я поставлю галочку на run as ‘low integrity’, я могу запустить программу hyperion нажав на кнопку, но программа hyperion запустится без отображения иконки в трее Windows, а если без галочки на run as ‘low integrity’, то нажав на кнопку, программа hyperion не запустится.

Так выглядит кнопка, которая при нажатии запускает программу

Так выглядит иконка в трее, когда просто запускаешь программу, а если запустим с run as ‘low integrity’, то такой иконки в трее не будет
image

Чтобы запустить программу без установленной галочки на run as ‘low integrity’ и чтобы была видна иконка программы в трее Windows, я запускаю программу через service: mqtt.publish таким образом

Узнать топик можно здесь

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: hyperiond

Вот тут самое интересное, в payload можно указать что угодно и все равно запустится hyperiond. Любой из вариантов ниже будет работать и программа hyperiond будет запускаться

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: C:\Program Files\Hyperion\bin\hyperiond.exe

---------------------------------------------------------------------------------------
service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: hyperiond

---------------------------------------------------------------------------------------
service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/pc_livingroom_hyperion_launch/action
  payload: ' '

Теперь следующее. Если в поле команды не указывать ничего и оставить поле пустым, то можно запустить любую программу через service: mqtt.publish, правда не любую программу. Использую Windows 11 x64.

Консоль cmd почему-то не запускается

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/LIVINGROOM_launching_program/action
  payload: '"C:\Windows\System32\cmd.exe'"

А блокнот запускается

service: mqtt.publish
data:
  topic: homeassistant/button/LIVINGROOM/LIVINGROOM_launching_program/action
  payload: '"C:\Program Files\Notepad++\notepad++.exe"'
2 Likes

Hi @DivanX10,

You’re beginning to understand HASS.Agent better than I do :grin: You’re right about everything, I’ll sum up the reasons etc:

  • When you create a CustomCommand, with the command defined within HASS.Agent, it will run ‘headless’, this means in the background without anything visible. This is ideal for commands that run and then close, but not so much for normal applications. I’ll add a ‘run normal’ checkbox, so you can use it to launch applications in normal mode. [hassagent-104]

  • When you run with ‘low integrity’, it will use a special method from within Windows to launch, which prevents it from writing to certain places on your harddrive. This is best for specific commands, not entire applications, because of the way it’s launched and restricted by Windows itself (I have no control over that).

  • When you don’t provide a command in HASS.Agent, but instead use the MQTT action topic, it will launch normal (within userspace, so visible). I’m not sure why the cmd won’t show, but that might be because the console doesn’t receive any actual command and closes itself.

If you need any more info (or changes), let me know :smiley:

1 Like

Thanks for sharing! I’ve added it to the docs. :heart:

I slightly altered the operation of the sensor for games by keywords, as I encountered a false trigger of the sensor for games when watching YouTube. On YouTube, keywords can match keywords for games and then when watching a game on YouTube, the game sensor will work. For example, keywords for games contain such words.

Halo Infinite|4 Engine|God of War|Forza Horizon

We launch YouTube with the game Halo Infinite and the game sensor will work for us, since the auxiliary element for the game has the keyword Halo Infinite. If we launch an online movie, the sensor game will work, although the game is not actually running.

What kind of content and where did it come from? Content is an auxiliary element text for keywords for content, everything that you watch through the browser, for example online movies, TV series, YouTube. This is how keywords for content look like

online|YouTube

Below is an example of a sensor that has a game check != content == ‘on’. If I watch YouTube or online movies and the keywords of the content match the keywords for the games, then the sensor game will not work

Note
ignorecase=True - ignoring the case comparison
ignorecase=False - don’t ignore the case comparison
games_kws - keywords for games
content_kws - keywords for content

Below is an example of a sensor for a game that has a check. If I watch YouTube or online movies and the keywords of the content match the keywords for the games, then the sensor game will not work

{% if game == True and content == False %} on

sensor:
  - platform: template
    sensors:
      hassagent_pc_livingroom_running_game:
        friendly_name: "Living room: Computer. Game"
        icon_template: mdi:google-controller
        value_template: >
            {% set activewindow = states("sensor.pc_livingroom_activewindow") %}
            {% set games_kws = states("input_text.pc_livingroom_games_keywords") %}
            {% set content_kws = states("input_text.pc_livingroom_content_keywords") %}
            {% set game = activewindow | string is search(find=games_kws, ignorecase=False) %}
            {% set content = activewindow | string is search(find=content_kws, ignorecase=False) %}
            {% if game == True and content == False %} on
            {% else %} off
            {% endif %}

Note that the template uses ignorecase=False\True. Why is this necessary and how does it work?

ignorecase is used to enable or disable case checking in words. We don’t always need a keyword search to be conducted for any case in words, but we need a keyword search to be conducted for a specific word and with a specific case. It all depends on the situation. I will give examples below

ignorecase=False - do not ignore the case comparison. The search will be case-sensitive and if the active sensor has HALO INFINITE, the word will not be detected, and if the active sensor has Halo Infinite, the word will be detected

Halo Infinite|4 Engine|God of War|Forza Horizon

ignorecase=True - ignore the case comparison. The search will be case-insensitive and if the active sensor has HALO INFINITY\Halo Infinity\HaLO INFINITE, the word will be detected regardless of which case it is specified in the keywords

HaLO INFINITE|4 EnGIne|God OF War|FORZa HoRIzon

To check the operation of ignorecase, you can use the options below

If we need to display a word instead of True\False

{{ 'HALO Infinite' | regex_findall("Halo Infinite") }}
{{ 'HALO Infinite' | regex_findall("HALO Infinite") }}
{{ 'HALO Infinite' | regex_findall("Halo Infinite", ignorecase=True) }}
{{ 'HALO Infinite' | regex_findall("Halo Infinite", ignorecase=False) }}

If we need to display True\False instead of the word

{{ 'HALO Infinite' | string is search("Halo Infinite") }}
{{ 'HALO Infinite' | string is search("HALO Infinite") }}
{{ 'HALO Infinite' | string is search(find="Halo Infinite", ignorecase=True) }}
{{ 'HALO Infinite' | string is search(find="Halo Infinite", ignorecase=False) }}

I will be glad if you offer your own version of the sensor for working with keywords

Текст на русском (Text in Russian)

Я не много переделал работу сенсора для игр по ключевым словам, так как столкнулся с ложным срабатыванием сенсора для игр при просмотре YouTube. На YouTube ключевые слова могут совпадать с ключевыми словами для игр и тогда при просмотре игры на YouTube сработает сенсор игра. Например в ключевых словах для игр указаны такие слова.

Halo Infinite|4 Engine|God of War|Forza Horizon

Запускаем YouTube с игрой Halo Infinite и у нас сработает сенсор игра, так как во вспомогательном элементе для игры имеется ключевое слово Halo Infinite. Если запустим онлайн фильм, то сработает сенсор игра, хотя игра не запущена в действительности.

Что за контент и откуда он взялся? Контент это вспомогательный элемент текст для ключевых слов для контента, все что смотрите через браузер, например онлайн фильмы, сериалы, YouTube. Вот так выглядят ключевые слова для контента

онлайн|YouTube

Примечание
ignorecase=True - игнорируем сравнение регистра
ignorecase=False - не игнорируем сравнение регистра
games_kws - ключевые слова для игр
content_kws - ключевые слова для контента

Ниже пример сенсора для игры который имеет проверку. Если смотрю YouTube или онлайн кино и ключевые слова контента совпадают с ключевыми словами для игр, то сенсор игра не сработает

{% if game == True and content == False %} on

sensor:
  - platform: template
    sensors:
      hassagent_pc_livingroom_running_game:
        friendly_name: "Гостиная: Компьютер. Игра"
        icon_template: mdi:google-controller
        value_template: >
            {% set activewindow = states("sensor.pc_livingroom_activewindow") %}
            {% set games_kws = states("input_text.pc_livingroom_games_keywords") %}
            {% set content_kws = states("input_text.pc_livingroom_content_keywords") %}
            {% set game = activewindow | string is search(find=games_kws, ignorecase=False) %}
            {% set content = activewindow | string is search(find=content_kws, ignorecase=False) %}
            {% if game == True and content == False %} on
            {% else %} off
            {% endif %}

Обратите внимание, что в шаблоне используется ignorecase=False\True. Зачем это нужно и как оно работает?

ignorecase используется для включения или выключения проверки регистра в словах. Не всегда нам нужно, чтобы поиск по ключевым словам велся на любой регистр в словах, а нужно, чтобы поиск по ключевым словам велся на конкретное слово и с конкретным регистром. Все зависит от ситуации. Ниже приведу примеры

ignorecase=False - не игнорируем сравнение регистра. Поиск будет с соблюдением регистра и если в активном сенсоре будет HALO INFINITE, то слово не будет обнаружено, а если активном сенсоре будет Halo Infinite, то слово будет обнаружено

Halo Infinite|4 Engine|God of War|Forza Horizon

ignorecase=True - игнорируем сравнение регистра. Поиск будет без соблюдения регистра и если в активном сенсоре будет HALO INFINITE\Halo Infinite\HaLO INFINITE, то слово будет обнаружено независимо в каком регистре оно указано в ключевых словах

HaLO INFINITE|4 EnGIne|God OF War|FORZa HoRIzon

Для проверки работы ignorecase можно использовать варианты ниже

Если нам нужно отображать слово вместо True\False

{{ 'HALO Infinite' | regex_findall("Halo Infinite") }}
{{ 'HALO Infinite' | regex_findall("HALO Infinite") }}
{{ 'HALO Infinite' | regex_findall("Halo Infinite", ignorecase=True) }}
{{ 'HALO Infinite' | regex_findall("Halo Infinite", ignorecase=False) }}

Если нам нужно отображать True\False вместо слова

{{ 'HALO Infinite' | string is search("Halo Infinite") }}
{{ 'HALO Infinite' | string is search("HALO Infinite") }}
{{ 'HALO Infinite' | string is search(find="Halo Infinite", ignorecase=True) }}
{{ 'HALO Infinite' | string is search(find="Halo Infinite", ignorecase=False) }}

Буду рад, если вы предложите свой вариант сенсора по работе с ключевыми словами

1 Like

I’ll be honest…I haven’t read your full post but figured I’d share a simpler way that I use for my gamesSensor. I created a custom WMIQuery that lets me know if the game’s process is running.

This is the full query for the games I monitor right now.

Select * from Win32_Process Where Name = ‘bf2042.exe’ OR Name = ‘HorizonZeroDawn.exe’ OR Name = ‘HaloInfinite.exe’ OR Name = ‘Cyberpunk2077.exe’ OR Name = ‘BlackOpsColdWar.exe’ OR Name = ‘ModernWarfare.exe’ OR Name = ‘GoW.exe’ OR Name = ‘reactivedrop.exe’

With this setup, it doesnt matter if the game is in the background…as long as it is running, the sensor will show the relevant process. This is what it looks like if I’m playing CyberPunk.

Works brilliantly for my use case.

2 Likes

Thanks. Tell me, is it possible to output the name of the game without exe? If so, how do I do it?

Not through the WMIQuery (at least not to my knowledge). You could make a template sensor off the one created by HassAgent. Curious as to why you would need it to report without the exe though.

In fact, it does not matter, it’s just possible to clarify the details or not. I need this to run presets on WLED. Some game has started and a preset for coolers is being launched

Here is a time-bound video, so that you don’t watch all the videos, but only the right part, where the preset for coolers is launched when the game starts

i tried all day to send a notification with image per called script and dont got it working.
the notification is sent, but without image.
Looks like its not able to use a template for the image in the action.

  action:
  - service: script.1655549409115
    data:
      title: '{{ camera_name }}'
      message: '{{ label }} - {{ direction }}'
      data:
        image: http://192.168.7:70:5000/api/events/{{trigger.payload_json['after']['id']}}/thumbnail.jpg

        image: http://192.168.1.222:8123/api/frigate/notifications/{{trigger.payload_json['after']['id']}}/thumbnail.jpg

if i call the script from dev tools using the same yaml with replaced {{texts}} it works.
I use the script to allow users to deactivate notifiations.

'1655549409115':
  alias: Frigate Nachricht PC - Self Selected
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: input_boolean.notify_pc_test
        state: 'on'
      sequence:
      - service: notify.hass_agent_test
        data:
          title: '{{ title }}'
          message: '{{ message }}'
          data: '{{ data }}'

whats wrong with data: '{{ data }}'?

I don’t use WLED so I don’t know how it integrates but a simple automation in HA that is triggered on the game sensor should allow you “activate” pretty much anything else integrated in HA.
eg.
If gamessensor=gow.exe then activate gow red lighting scheme
If gamessensor=haloinfinite.exe then activate halo blue lighting scheme

Almost so :grinning: , but as I wrote above, there is no special difference, whether there is an exe or not, it doesn’t matter, you can just specify the names of games in presets in this form HaloInfinite.exe

The sensor of the game itself is specified in the trigger and when the trigger is triggered, a command is sent to WLED as is and will launch a preset with the name HaloInfinite.exe

service: select.select_option
data:
  option: '{{ states("sensor.pc_livingroom_name_running_game") }}'
target:
  entity_id: select.pc_livingroom_preset

when the game is turned off, it starts

service: select.select_option
data:
  option: Static 01
target:
  entity_id: select.pc_livingroom_preset

Very nice, also very informative to learn about value templates!

Thanks for sharing, I’ve added a link to the new version to the docs.

Thanks @stephack, added to the WMI examples.

Hi @vajdum,

Could you please check what the logs say? You can open the logs folder here:

If there’s nothing relevant, enable extended logging, restart and try again.

HASS.Agent is probably receiving an URL that it can’t (or isn’t allowed to) reach. The goal is to send an URL, like http://192.168.1.1/image.jpg, which HASS.Agent will then download and show.

You could also try setting a variable first, like this example:

notification_test:
  alias: Notification Test
  variables:
    image: |
      {%- set image = "http://hass.local:8123" + state_attr("camera.garden","entity_picture") %}
      {{ image }}
  sequence:
  - service: notify.hass_agent_test
    data:
      title: Test
      message: "This is a test message with an image."
      data:
        image: "{{ image }}"
  mode: single
  icon: mdi:bell

So I’m just starting to mess with HASS.agent and my HA. I’m trying to set an automation to fire and one of the conditions would be that my headphones are the primary audio, rather than my desktop speakers. Any suggestion on how I would create that entity?

Hi @Peter_Uhl,

Start by making an ‘Audio’ sensor in HASS.Agent, this is a multivalue sensor so it’ll add a bunch of new sensors to your device in HA. The one you want is: sensor.your_pc_name_audio_default_device. It’ll contain the name of your default audio device.

You can then use it as a condition for your automation, something like this:

condition:
  condition: state
  entity_id: sensor.your_pc_name_audio_default_device
  state: "The Name Of Your Audio Device"

Haven’t tested it, but should work - let me know if it doesn’t!

If you want an intro into what HASS.Agent can do, you can use this doc:

https://hassagent.readthedocs.io/en/latest/introduction/

And optionally this intro for commands:

https://hassagent.readthedocs.io/en/latest/commands/command-basics/

Do you know what the condition of the state would be. If my “default audio device” is the desktop speakers, what condition would I set it to, to recognize when the audio device are my headphones

That depends on what it’s called. Just see what value the sensor has for the activated device. For instance, when I enabled my (usb) headset, this is what I get:

image

And when I set it back to speakers, it becomes this:

image