Mystrom button authentication

I’m trying to use the MyStrom buttons, and these work through an API call back to home assistant. I’m struggling with the authentication. I have an API password set:

  auth_providers:
    - type: homeassistant
    - type: trusted_networks
      trusted_networks:
        - 192.168.0.0/21
        - 192.168.1.40
        - 172.17.0.0/24
        - fd00::/8
    - type: legacy_api_password
      api_password: !secret api_password

However when I try to test the API with curl, I get 401 unauthorized:

$ curl -X GET -k "https://walker:8123/api/mystrom?api_password=##########&single=myStromButton01"
401: Unauthorized

I can get it working with a header and a bearer token:

$ curl -X GET -k -H 'Authorization: Bearer <LongToken>' "https://walker:8123/api/mystrom?single=myStromButton01"

however I don’t know how I can put a header into the MyStrom button config (I seem to only be able to program this with the URL (and the docs say to put api_password=##### into the URL, which is not working. The button (and the computer I was running curl from) are in the trusted network ranges.

Is there a way I can allow the switches to work without the token (using either api-password or by exempting them from authorisation?

Hi,
I tried to make my wifi buttons work with Home Assistant for days, but without success.

In addition it is written that the legacy_api_password will be dropped in a future release (See legacy-api-password)

I was so tired that I wrote a gateway mystrom button to MQTT.

I use the new generic action that comes with the firmware 2.74.10.

I put the code to the gateway here:

Best regards

1 Like

Thanks for the advice, and especially for sharing the code!!

Looks great, I will give it a try thanks! Too bad mystrom devices cannot handle Bearer tokens, which are the standard way to deal with authentication :frowning_face:

mystrom2mqtt is a work-around for now.

Thanks @fabaff, is there a possibility to install this as a Home Assistant Add On?

I created an addon. It was working back then but there was zero feedback and I never released it.

1 Like

Hi all,
I’m quite satisfied using myStrom wifi button with HA webhooks.
It’s natively available, no add-ons are needed and perfectly solve the API password issue.
I configured different request for any action on wifi button:

curl --location --request POST 'http://192.168.202.155/api/v1/action/single' \
--data-raw 'post://192.168.202.151:8123/api/webhook/mystrom_b1?action=single'

curl --location --request POST 'http://192.168.202.155/api/v1/action/double' \
--data-raw 'post://192.168.202.151:8123/api/webhook/mystrom_b1?action=double'

curl --location --request POST 'http://192.168.202.155/api/v1/action/generic' \
--data-raw 'post://192.168.202.151:8123/api/webhook/mystrom_b1?action=generic'

so I can manage them from HA.
I can see POST requests coming from myStrom button:

action <MultiDictProxy(‘action’: ‘single’)>
action <MultiDictProxy(‘action’: ‘generic’, ‘mac’: ‘68C63AD0xxyy’, ‘action’: ‘6’, ‘battery’: ‘26’)>

In order to avoid many automations, I can use just one automation and, with a switch case statement like, using conditional actions, something like:

action:
  if trigger.query.action = 'single' >> script.single
  if trigger.query.battery is present >> script.notify.low.battery if < 10%

and so on.
Any hint about how to use URL parameters for conditional actions?
Thanks,
-f

2 Likes

Hi @rosseba, exactly same issue with standard call (get + login error). I will try your solution.
I would be happy to see if you get any response with your question of URL parameters for conditional actions. Kind regards,
Romain

A brief update:
using input_number I’m tracking button battery, below the config I’m using.
myStrom button trigger generic request (with battery status as well) every 12h-ish.

configuration.yaml file:

input_number:
  mystrom_b1_3p:
    name: myStrom b1 3p
    min: 0
    max: 100
    step: 1
    icon: mdi:battery
    unit_of_measurement: '%'

automations.yaml file:

- id: '1610140149607'
  alias: mystrom_b1_3p
  description: ''
  trigger:
  - platform: webhook
    webhook_id: mystrom_b1
  condition: []
  action:
  - service: input_number.set_value
    data:
      value: '{{ trigger.data.battery | int }}'
    entity_id: input_number.mystrom_b1_3p
  mode: single
1 Like

I’m also looking for a simple integration as a HACS addon; (

Hi

I just setup my buttons using webhooks as @rosseba suggested. After a while I have now all together. If somebody is interested, here is what I did.

  • I’m using the following dummy values. Replace them with yours:
    • Button ID: AABBCCDDEEFF → the MAC address of the button
    • Button IP: 192.168.1.22
    • Homeassistant socket: 192.168.1.10:8123
    • Webhook ID: mystrom_living_b1
  • MyStrom Button
    • Restart the button (take off one battery and put it back in again)
    • The button accepts now a new configuration for about 3min
    • Remove the previous configuration
      curl -d "single=&double=&long=&touch=&generic=" http://192.168.1.22/api/v1/device/AABBCCDDEEFF
      
    • Set the new generic post command to the webhook API
      curl --location --request POST 'http://192.168.1.22/api/v1/action/generic' --data-raw 'post://192.168.1.10:8123/api/webhook/mystrom_living_b1?method=generic'
      
    • Setup an automation
      alias: MyStromButtons
      description: ''
      trigger:
        - platform: webhook
          webhook_id: mystrom_living_b1
          id: livingroom_b1
      condition: []
      action:
        - choose:
            - conditions:
                - condition: and
                  conditions:
                    - condition: trigger
                      id: livingroom_b1
                    - condition: template
                      value_template: '{{ trigger.data.action|int == 1 }}'
              sequence:
                - service: light.toggle
                  target:
                    entity_id: light.light_1
            - conditions:
                - condition: and
                  conditions:
                    - condition: trigger
                      id: livingroom_b1
                    - condition: template
                      value_template: '{{ trigger.data.action|int == 2 }}'
              sequence: []
            - conditions:
                - condition: and
                  conditions:
                    - condition: trigger
                      id: livingroom_b1
                    - condition: template
                      value_template: '{{ trigger.data.action|int == 3 }}'
              sequence:
                - service: light.toggle
                  target:
                    entity_id: light.light_2
            - conditions:
                - condition: and
                  conditions:
                    - condition: trigger
                      id: livingroom_b1
                    - condition: template
                      value_template: '{{ trigger.data.action|int == 4 }}'
              sequence:
                - service: light.toggle
                  target:
                    entity_id:
                      - light.light_1
                      - light.light_2
            - conditions:
                - condition: and
                  conditions:
                    - condition: trigger
                      id: livingroom_b1
                    - condition: template
                      value_template: '{{ trigger.data.action|int == 6 }}'
              sequence:
                - service: input_number.set_value
                  target:
                    entity_id: input_number.mystrombuttonlivingroom
                  data:
                    value: '{{ trigger.data.battery|int }}'
          default: []
        - choose:
            - conditions:
                - condition: trigger
                  id: livingroom_b1
                - condition: template
                  value_template: '{{ "battery" in trigger.data }}'
              sequence:
                - service: input_number.set_value
                  target:
                    entity_id: input_number.mystrombuttonlivingroom
                  data:
                    value: '{{ trigger.data.battery|int }}'
          default: []
      mode: single
      
      

Some comments to the automation:

  • The automation can be used for multiple buttons. Just extend the triggers and the conditions with the new webhook ids
  • The action id represents the button action
    • SINGLE = 1
    • DOUBLE=2
    • LONG=3
    • TOUCH=4
    • WHEEL=5
    • WHEEL_FINAL=11
    • BATTERY=6
  • A received message normally contains the following information
    {'method': 'generic', 'mac': 'AABBCCDDEEFF', 'action': '1', 'battery': '100'}
    
  • Since every message normally contains the battery level, there is a second action in the automation which checks whether battery is present. If yes, the corresponding input_number is updated with the current battery level.

Regards
Mathias

5 Likes

I am struggling with this part. how can I check if the button has received this coding correctly? It shows after clicking first white, then green, then a red light. In HA it seems that the request comes not through. Via postman I can trigger the hook.
Can I maybe set the URL in the app?

Via curl this comes back from the button for the coding:

“generic”: “post://x.com/api/webhook/mystrom_b1_wh?method=generic”

Can I maybe set the URL in the app?

Unfortunately, no.

Via curl this comes back from the button for the coding:

“generic”: “post://x.com/api/webhook/mystrom_b1_wh?method=generic”

Is this what you sent before? If yes, then from the MyStrom button point of view, it should be fine.

Do you have a possibility to run a tcpdump or wireshark to check whether your homeassistant receives the message? Your homeassistant runs on port 443 or 80? In my case, homeassistant runs on port 8123 in plain mode. I’m using a reverse proxy in front of it to encrypt the traffic. I’m not sure if the MyStrom Buttons support TLS…

I found it!
I used a domain which did not work properly. For this use case I guess it is fine to directly point to a IP. Still wondering why it did not work correctly. Maybe my reverse proxy need some tweaks.

My final solution attached with the addon to toggle per button function a boolean. Is there room for improvement? Can I somehow aggregate code to make it cleaner?

config

input_boolean:
  button1_short_pressed:
    name: button1_short_pressed
    initial: off
  button1_long_pressed:
    name: button1_long_pressed
    initial: off
  button1_double_pressed:
    name: button1_double_pressed
    initial: off

automation

- id: my-strom-button
  alias: MyStromButtons
  description: ''
  trigger:
    - platform: webhook
      webhook_id: mystrom_b1_wh
      id: mystrom_b1
  condition: []
  action:
    - choose:
          - conditions:
              - condition: and
                conditions:
                  - condition: trigger
                    id: mystrom_b1
                  - condition: template
                    value_template: '{{ trigger.data.action|int == 1 }}'
            sequence:
               - service: input_boolean.toggle
                 target:
                   entity_id: input_boolean.button1_short_pressed
          - conditions:
              - condition: and
                conditions:
                  - condition: trigger
                    id: mystrom_b1
                  - condition: template
                    value_template: '{{ trigger.data.action|int == 2 }}'
            sequence: 
               - service: input_boolean.toggle
                 target:
                   entity_id: input_boolean.button1_double_pressed
          - conditions:
              - condition: and
                conditions:
                  - condition: trigger
                    id: mystrom_b1
                  - condition: template
                    value_template: '{{ trigger.data.action|int == 3 }}'
            sequence:
               - service: input_boolean.toggle
                 target:
                   entity_id: input_boolean.button1_long_pressed
          - conditions:
              - condition: and
                conditions:
                  - condition: trigger
                    id: mystrom_b1
                  - condition: template
                    value_template: '{{ trigger.data.action|int == 6 }}'
            sequence:
              - service: input_number.set_value
                target:
                  entity_id: input_number.mystrombuttonlivingroom
                data:
                  value: '{{ trigger.data.battery|int }}'
      default: []
    - choose:
          - conditions:
              - condition: trigger
                id: mystrom_b1
              - condition: template
                value_template: '{{ "battery" in trigger.data }}'
            sequence:
              - service: input_number.set_value
                target:
                  entity_id: input_number.mystrombuttonlivingroom
                data:
                  value: '{{ trigger.data.battery|int }}'
      default: []

- id: button1-pressed-short-on 
  trigger:
    platform: state
    entity_id: input_boolean.button1_short_pressed
    to: 'on'
  action:
     - service: media_player.play_media
       data:
           media_content_id: x
           media_content_type: routine
       target:
           entity_id: media_player.echo 

- id: button1-pressed-short-off
  trigger:
    platform: state
    entity_id: input_boolean.button1_short_pressed
    to: 'off'
  action:
     - service: media_player.play_media
       data:
           media_content_id: x
           media_content_type: routine
       target:
           entity_id: media_player.echo 
           
- id: button1-pressed-long-off 
  trigger:
    platform: state
    entity_id: input_boolean.button1_long_pressed
    to: 'off'
  action:
     - service: media_player.play_media
       data:
           media_content_id: x 
           media_content_type: routine
       target:
           entity_id: media_player.echo 
           
- id: button1-pressed-long-on 
  trigger:
    platform: state
    entity_id: input_boolean.button1_long_pressed
    to: 'on'
  action:
     - service: media_player.play_media
       data:
           media_content_id: x 
           media_content_type: routine
       target:
           entity_id: media_player.echo 

- id: button1-pressed-double-off 
  trigger:
    platform: state
    entity_id: input_boolean.button1_double_pressed
    to: 'off'
  action:
     - service: media_player.play_media
       data:
           media_content_id: x 
           media_content_type: routine
       target:
           entity_id: media_player.echo 
           
- id: button1-pressed-double-on 
  trigger:
    platform: state
    entity_id: input_boolean.button1_double_pressed
    to: 'on'
  action:
     - service: media_player.play_media
       data:
           media_content_id: x 
           media_content_type: routine
       target:
           entity_id: media_player.echo

Hello

Thank you for your share

When I try to use this command

‘’’
curl --location --request POST ‘http://192.168.1.22/api/v1/action/generic’ --data-raw ‘post://192.168.1.10:8123/api/webhook/mystrom_living_b1?method=generic’
‘’’

I’m receiving a error

image

Any Ideia how to solve this ?

Hi

Can you post the exact command which you used?

Yes, and thank you for your reply

I googled a bit and some are mentioning, that you should use double quotes for the url instead of single quotes. Can you give a try?

Something like:

curl --location --request POST "http://192.168.1.22/api/v1/action/generic" --data-raw 'post://192.168.1.10:8123/api/webhook/mystrom_living_b1?method=generic'